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>