博客
关于我
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/

    你可能感兴趣的文章
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    NR,NF,FNR
    查看>>
    nrf开发笔记一开发软件
    查看>>
    NSDateFormatter的替代方法
    查看>>
    NSOperation基本操作
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    NT AUTHORITY\NETWORK SERVICE 权限问题
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    nullnullHuge Pages
    查看>>
    numpy 用法
    查看>>
    Numpy如何使用np.umprod重写range函数中i的python
    查看>>
    oauth2-shiro 添加 redis 实现版本
    查看>>
    OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
    查看>>
    OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
    查看>>
    OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
    查看>>
    OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
    查看>>
    OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
    查看>>
    OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
    查看>>
    OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
    查看>>