美文网首页
java_过滤器|fliter

java_过滤器|fliter

作者: 走停2015_iOS开发 | 来源:发表于2021-02-05 15:58 被阅读0次
    功能
    1. 用于拦截传入的请求和传出的响应
    2. 修改或以某种方式处理正在客户端和服务端之间的数据流
    如何使用

    fliter与使用Servlet类似,是java WEB提供的一个接口,开发者只需要自定义一个类并且实现该接口即可

    1. 注解方式
    @WebFilter("/filter_Servlet")
    public class myfliter implements Filter {
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        }
    }
    
    @WebServlet("/filter_Servlet")
    public class filterServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name = (String) req.getAttribute("name");
            System.out.println(name);
        }
    }
    
    2. web.xml中配置filter

    拦截单个servlet

        <filter>
            <filter-name>myfliter</filter-name>
            <filter-class>com.example.qihonwangjava.myfliter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>myfliter</filter-name>
            <url-pattern>/filter_Servlet</url-pattern>
        </filter-mapping>
    

    拦截多个servlet

        <filter>
            <filter-name>myfliter</filter-name>
            <filter-class>com.example.qihonwangjava.myfliter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>myfliter</filter-name>
            <url-pattern>/filter_Servlet</url-pattern>
            <url-pattern>/myServlet</url-pattern>
        </filter-mapping>
    
    2. filter类和sevlet

    filterChain.doFilter(servletRequest,servletResponse);
    必须添加 否则请求会一直停留在过滤器中 无法进入servlet

    public class myfliter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws    ServletException {
        }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("utf-8");
            servletRequest.setAttribute("name","祁红旺");
            filterChain.doFilter(servletRequest,servletResponse);
        }
        @Override
        public void destroy() {
        }
    }
    
    @WebServlet("/filter_Servlet")
    public class filterServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println(req.getAttribute("name"));
        }
    }
    
    生命周期
    public class myfliter implements Filter {
        public myfliter() {
            System.out.println("通过反射机制调用无参构造创建了对象");
        }
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("调用init");
        }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("utf-8");
            servletRequest.setAttribute("name","祁红旺");
            filterChain.doFilter(servletRequest,servletResponse);
        }
        @Override
        public void destroy() {
        }
    }
    
    打印结果
     1、通过反射机制调用无参构造创建了对象
     2、调用init
    总结
    1、tomcat服务启动通过web.xml的配置进行反射机制自动就创建对象同时初始化 当服务关闭的时候调用destroy销毁对象
    2、servlet是调用请求的时候才去创建对象
    
    实际开发中filter的使用场景
    • 统一处理中文乱码
    • 屏蔽敏感词
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //将敏感词替换成“***”
            servletRequest.setCharacterEncoding("utf-8");
            String name =  servletRequest.getParameter("name");
            name= name.replaceAll("敏感词","***");
            //通过attribute替换Parameter
            servletRequest.setAttribute("name",name);
        }
    
    @WebServlet("/filter_Servlet")
    public class filterServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name = (String) req.getAttribute("name");
            System.out.println(name);
        }
    }
    
    • 控制资源的访问权限(检测用户是否是登录状态 如果是登录状态才让跳转下载页面)

    相关文章

      网友评论

          本文标题:java_过滤器|fliter

          本文链接:https://www.haomeiwen.com/subject/fnbbtltx.html