JavaWeb中的filter链
过滤器链(配置多个过滤器)
- 执行顺序:如果有两个过滤器:过滤器1和过滤器2
- 过滤器1
- 过滤器2
- 资源执行
- 过滤器2
- 过滤器1
- 过滤器先后顺序问题:
- 注解配置:按照类名的字符串比较规则比较,值小的先执行
如: AFilter 和 BFilter,AFilter就先执行了。 - web.xml配置:
谁定义在上边,谁先执行
- 注解配置:按照类名的字符串比较规则比较,值小的先执行
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//过滤器链
@WebFilter("/*")
public class FilterDemo6 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("demo6....go...");
chain.doFilter(req, resp);
System.out.println("demo6....back...");
}
public void init(FilterConfig config) throws ServletException {
}
}
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo7 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("demo7....go...");
chain.doFilter(req, resp);
System.out.println("demo7....back...");
}
public void init(FilterConfig config) throws ServletException {
}
}