博客
关于我
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 | 如何用Python锁避免并发错误?
    查看>>
    python | 提升代码迭代速度的Python重载方法
    查看>>
    python | 深入理解Python并发编程中的GIL限制与解决方案
    查看>>
    Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
    查看>>
    python | 高效使用Python工具自动生成模块文档的秘诀
    查看>>
    python 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    python读取word表格内容(1)
    查看>>
    python 中os.path.join 双斜杠的解决办法
    查看>>
    python 中PIL.Image和OpenCV图像格式相互转换
    查看>>
    Python 中Semaphore 信号量对象、Event事件、Condition
    查看>>
    python 中with的使用及样例
    查看>>
    python读取wav文件并播放[pyaudio/wave]
    查看>>
    python读取txt文件的行数
    查看>>
    Python 中内置的最大堆 API
    查看>>