博客
关于我
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 if,循环的练习
    查看>>
    Python in open course (week3)
    查看>>
    Python IO编程
    查看>>
    Python IO编程详解
    查看>>
    Python IPL
    查看>>
    python join split
    查看>>
    python json文件传输图片
    查看>>
    python kivy AttributeError:‘super‘对象没有属性‘__getattr__‘
    查看>>
    Python Kivy ListView:如何删除选定的 ListItemButton?
    查看>>
    Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll
    查看>>
    Python Kivy库:跨平台应用开发
    查看>>
    python lambda表达式
    查看>>
    Python ldap3 代码从 SID 获取用户名
    查看>>
    Python list += iterable 的行为是否记录在任何地方?
    查看>>
    python list,str的拼接与转换
    查看>>
    python list函数使用总结_史上最全的Python数据结构:列表和元组用法总结
    查看>>
    python locust 性能测试:locust参数-保证并发测试数据唯一性,循环取数据
    查看>>
    python locust 性能测试:locust安装和一些参数介绍
    查看>>
    python log
    查看>>
    python logging basicconfig_python之logging.basicConfig
    查看>>