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;
        }
    }
  • 相关阅读:
    解决问题通用方法论
    Flutter 即学即用系列博客总结篇
    Android Q 兼容那些事
    Android 截屏的各种骚操作
    MTLTexture转成UIimage
    swift使用metal加载三角形、平面图片、立体图像
    GPUImage2的使用
    Swift OpenGL ES 自定义常用滤镜(二)
    Swift OpenGL ES 自定义常用滤镜(一)
    Core Image简介与使用
  • 原文地址:https://www.cnblogs.com/rayguo/p/3871614.html
Copyright © 2011-2022 走看看