JavaWeb中的filter链

过滤器链(配置多个过滤器)

  1. 执行顺序:如果有两个过滤器:过滤器1和过滤器2
    1. 过滤器1
    2. 过滤器2
    3. 资源执行
    4. 过滤器2
    5. 过滤器1
  2. 过滤器先后顺序问题:
    1. 注解配置:按照类名的字符串比较规则比较,值小的先执行
      如: AFilter 和 BFilter,AFilter就先执行了。
    2. 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 {

    }

}


posted @ 2021-05-11 21:47  code-G  阅读(128)  评论(0编辑  收藏  举报