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

    你可能感兴趣的文章
    Nvidia驱动失效,采用官方的方法重装更快
    查看>>
    nvm切换node版本
    查看>>
    nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
    查看>>
    ny540 奇怪的排序 简单题
    查看>>
    NYOJ 1066 CO-PRIME(数论)
    查看>>
    nyoj------203三国志
    查看>>
    nyoj58 最少步数
    查看>>
    OAuth2 + Gateway统一认证一步步实现(公司项目能直接使用),密码模式&授权码模式
    查看>>
    OAuth2 Provider 项目常见问题解决方案
    查看>>
    Vue.js 学习总结(14)—— Vue3 为什么推荐使用 ref 而不是 reactive
    查看>>
    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
    查看>>
    OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
    查看>>
    OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
    查看>>