Filter过滤器在web项目中的使用。

1.实现Filter接口

public class TestFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = RequestUtils.toHttp(req);
        HttpServletResponse response = ResponseUtils.toHttp(res);
       //逻辑处理代码
    
        chain.doFilter(request, response);
    }

}
    

  

 

 

 

2.web.xml中配置

    <filter>
        <filter-name>testFilter</filter-name> //自定义过滤器名
        <filter-class>com.java.TestFilter</filter-class> //类
        <async-supported>true</async-supported> //异步支持
    </filter>
    <filter-mapping>
        <filter-name>testFilter</filter-name> 
        <url-pattern>/*</url-pattern> //作用范围
    </filter-mapping>

 

posted @ 2020-04-07 20:32  埼玉howfree  阅读(390)  评论(0编辑  收藏  举报