zoukankan      html  css  js  c++  java
  • 过滤器Filter

    什么是过滤器

      过滤器Filter其实就是Servlet,只不过它的职责是在过滤信息,在请求进入核心servlet时候的过滤,在响应发送给客户端时候的过滤。

    使用过滤器

      要想创建自己的Filter,只需要实现Filter接口,重写里面的init方法、doFilter方法、destroy方法。init方法在你进行过滤的时候的初始化方法,doFilter方法就是你的核心过滤方法,里面有两个参数,一个是ServletRequest,另外一个是ServletResponse,前面一个代表的是请求,后面一个代表的是响应,我们的过滤操作就是对请求和响应的信息进行过滤。

    过滤器的模式

      一般的过滤器都不是一个,多个过滤器就组成了一个过滤链,一个过滤器执行了自己的doFilter方法后,就可以通过参数FilterChain调用下一个过滤器,这样一个过滤链就形成了。在FilterChian调用下一个doFilter前面过滤的是请求,后面过滤的是响应。

          

    编码过滤器

    /**
     * 编码过滤器
     * @author xujianguo <Ray_xujianguo@163.com>
     * @date 2014-6-12
     * @CopyRight 2014 Topview Inc.
     * @version V1.0
     */
    @WebFilter(urlPatterns={"/*"})
    public class EncodingFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("------ EncodingFilter Init ------");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("UTF-8");
            chain.doFilter(request, response);
            response.setCharacterEncoding("UTF-8");
        }
    
        @Override
        public void destroy() {
            System.out.println("------ EncodingFilter Destroy ------");
        }
    }

    权限过滤器

    /**
     * 登陆过滤器
     * @author xujianguo <Ray_xujianguo@163.com>
     * @date 2014-6-12
     * @CopyRight 2014 Topview Inc.
     * @version V1.0
     */
    @WebFilter(filterName="is_login", urlPatterns={"/*"}, 
        initParams={
            @WebInitParam(name="loginPage", value="/login.jsp"),
            @WebInitParam(name="login", value="/login")
        }
    )
    public class IsLoginFilter implements Filter {
        //过滤器配置
        private FilterConfig config;
        
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            //初始化
            this.config = filterConfig;
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            //获取登陆页面的路径
            String loginPage = config.getInitParameter("loginPage");
            String login = config.getInitParameter("login");
            //强制转换请求
            HttpServletRequest httpRequest = (HttpServletRequest)request;
            //获取请求的路径
            String requestPath = httpRequest.getServletPath();
            //获取Session
            HttpSession session = httpRequest.getSession();
            
            //判断是否登陆
            if(session.getAttribute("loginUser") == null && !requestPath.endsWith(loginPage) && !requestPath.endsWith(login)) {
                //跳转页面
                request.getRequestDispatcher(loginPage).forward(request, response);
            } else {
                //继续执行
                chain.doFilter(request, response);
            }
        }
    
        @Override
        public void destroy() {
            //销毁
            config = null;
        }
    }
  • 相关阅读:
    洛谷P3676 小清新数据结构题 【树剖 + BIT】
    CSU1911 Card Game 【FWT】
    CF662C Binary Table 【状压 + FWT】
    hdu5909 Tree Cutting 【树形dp + FWT】
    BZOJ4589 Hard Nim 【FWT】
    BZOJ2436 [Noi2011]Noi嘉年华 【dp】
    BZOJ3142 [Hnoi2013]数列 【组合数学】
    BZOJ2878 [Noi2012]迷失游乐园 【基环树 + 树形dp + 期望dp】
    BZOJ2437 [Noi2011]兔兔与蛋蛋 【博弈论 + 二分图匹配】
    BZOJ1443 [JSOI2009]游戏Game 【博弈论 + 二分图匹配】
  • 原文地址:https://www.cnblogs.com/rayguo/p/3871614.html
Copyright © 2011-2022 走看看