博客
关于我
JavaWeb快速入门--Filter&Listener
阅读量:60 次
发布时间:2019-02-25

本文共 3615 字,大约阅读时间需要 12 分钟。

JavaWeb ???????????

???

????JavaWeb????????????????????????????????????????????????????????????????????????????????????????

????????

  • ????

    ???????????????????

    • REQUEST??????????
    • FORWARD???RequestDispatcher#forward()???????
    • INCLUDE???RequestDispatcher#include()???????
    • ERROR???????????????????
  • ????

    ?????????????????

    • ???????/index.jsp
    • ???/user/*
    • ????*.jsp
    • ?????/*
  • ????????

    ???????????

    • init(FilterConfig)????????????????
    • doFilter(ServletRequest, ServletResponse, FilterChain)?????????????
    • destroy()????????????
  • ????????????

    @WebFilter("/*")public class CharacterFilter implements Filter {    @Override    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) req;        HttpServletResponse response = (HttpServletResponse) res;                if (request.getMethod().equalsIgnoreCase("post")) {            request.setCharacterEncoding("utf-8");        }        response.setContentType("text/html;charset=utf-8");                chain.doFilter(request, response);    }}

    ???????????

    @WebFilter("/*")public class SensitiveWordsFilter implements Filter {    private List
    sensitiveWords = new ArrayList<>(); @Override public void init(FilterConfig config) throws ServletException { try { ServletContext servletContext = config.getServletContext(); String path = servletContext.getRealPath("/WEB-INF/classes/sensitiveWords.txt"); BufferedReader br = new BufferedReader(new FileReader(path)); while ((line = br.readLine()) != null) { sensitiveWords.add(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { ServletRequest proxyRequest = new Proxy(req) { @Override public String getParameter() { String value = super.getParameter(); if (value != null) { for (String word : sensitiveWords) { if (value.contains(word)) { value = value.replaceAll(word, "***"); } } } return value; } }; chain.doFilter(proxyRequest, resp); }}

    ???

    ????JavaWeb???????????Web????????????????????Servlet??8?????????????????

  • ServletContextListener???ServletContext?????????
  • SessionListener???session???????
  • RequestListener???request???????
  • ??????ServletContextListener

    @WebListenerpublic class ContextLoaderListener implements ServletContextListener {    @Override    public void contextInitialized(ServletContextEvent event) {        ServletContext servletContext = event.getServletContext();        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");        String realPath = servletContext.getRealPath(contextConfigLocation);        try {            FileInputStream fis = new FileInputStream(realPath);            System.out.println("ServletContext????????????" + realPath);        } catch (IOException e) {            e.printStackTrace();        }    }    @Override    public void contextDestroyed(ServletContextEvent event) {        System.out.println("ServletContext??????");    }}

    ????????

    • ServletContextListener?????????????????
    • SessionListener?????????????????????????
    • RequestListener??????????????????????????????

    ????????????????????JavaWeb???????????????

    转载地址:http://uim.baihongyu.com/

    你可能感兴趣的文章
    Python Pandas-从DataFrame按类别绘制多个条形图
    查看>>
    Python Pandas:每月或每周拆分 TimeSerie
    查看>>
    python pandas中融化的对面
    查看>>
    python pandas从时间序列中提取唯一日期
    查看>>
    python pandas库详解_Pandas 库的详解和使用补充
    查看>>
    Python Pandas滚动聚合一列列表
    查看>>
    python pandas相关知识点(练习)
    查看>>
    Python Pandas,从.groupby().Apply()中的GROUP中分割行
    查看>>
    Python pathlib模块详解:优雅处理文件路径
    查看>>
    python Path模块的使用 glob iglob name
    查看>>
    python pickle 模块的使用
    查看>>
    Python PIL/Pillow-Pad图像至所需大小(例如,A4)
    查看>>
    python PIL模框使用
    查看>>
    Python ping 模块
    查看>>
    Python Pingouin:搞定各种假设检验和统计模型 !
    查看>>
    Python pip 国内镜像大全及使用办法
    查看>>
    Python输入输出练习,运算练习,turtle初步练习
    查看>>
    Python pip工具使用
    查看>>
    Python pip配置国内源
    查看>>
    Python Plotly 将轴数格式化为 %
    查看>>