@WebFilter(filterName = "Filter01",value = "/*")
public class Filter01 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("过滤器开始");
chain.doFilter(req, resp);
System.out.println("执行完了");
}
public void init(FilterConfig config) throws ServletException {
}
}
主页init 调用了2次后端
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("拦截了"); chain.doFilter(req, resp); }
看这个方法参数,请求和响应调用了一次,Filter调用一次,而/*是过滤所有,所有会打印两次。