过滤器拦截路径配置以及过滤器拦截方式配置和过滤器链

过滤器拦截路径配置

1、具体资源路径:/index.jsp  只有访问index.jsp资源时,过滤器才会被执行

2、拦截目录:/user/*  访问/user下的所有资源时,过滤器都会被执行

3、后缀名拦截:*.jsp  访问所有后缀名为jsp资源时,过滤器都会被执行

4、拦截所有资源:/*  访问所有资源时,过滤器都会被执行

复制代码
//@WebFilter("/index.jsp")//1、具体资源路径:/index.jsp  只有访问index.jsp资源时,过滤器才会被执行
//@WebFilter("/user/*")//2、拦截目录:/user/*  访问/user下的所有资源时,过滤器都会被执行
//@WebFilter("*.jsp")//3、后缀名拦截:*.jsp  访问所有后缀名为jsp资源时,过滤器都会被执行
@WebFilter("/*")//4、拦截所有资源:/*  访问所有资源时,过滤器都会被执行
public class FilterDemo4 implements Filter {
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("FilterDemo4...");
        //放行
        chain.doFilter(req, resp);
    }
    
    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }
}
复制代码

 

 

 

 

 

 

 

过滤器拦截方式配置

拦截方式配置:资源被访问的方式

注解配置:

  设置dispatcherTypes属性

    1、REQUEST:默认值。浏览器直接请求资源

    2、FORWARD:转发访问资源

    3、INCLUDE:包含访问资源

    4、ERROR:错误跳转资源

    5、ASYNC:异步访问资源

复制代码
//浏览器直接请求资源时,该过滤器会被执行
//@WebFilter(value = "/*",dispatcherTypes = DispatcherType.REQUEST)
//只有转发访问index.jsp时,该过滤器才会被执行
//@WebFilter(value = "/*",dispatcherTypes = DispatcherType.FORWARD)

//浏览器直接请求index.jsp页面或者转发访问index.jsp页面,过滤器会执行
@WebFilter(value = "/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
public class FilterDemo5 implements Filter {
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("FilterDemo5...");
        //放行
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }
}
复制代码

web.xml配置:

设置:<dispatcher></dispatcher>标签

复制代码
    <filter>
        <filter-name>filedemo1</filter-name>
        <filter-class>com.tomcat1.web.filter.FilterDemo1</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filedemo1</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
复制代码

 

 

 

 

 

 

 

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

执行顺序:如果有两个过滤器:过滤器1和过滤器2

  1、过滤器1

  2、过滤器2

  3、资源执行

  4、过滤器2

  5、过滤器1

复制代码
@WebFilter("/*")
public class FilterDemo6 implements Filter {
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("FilterDemo6执行了...");
        //放行
        chain.doFilter(req, resp);
        System.out.println("FilterDemo6返回...");
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }
}
复制代码
复制代码
@WebFilter("/*")
public class FilterDemo7 implements Filter {
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("FilterDemo7执行了...");
        //放行
        chain.doFilter(req, resp);
        System.out.println("FilterDemo7返回...");
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }
}
复制代码

 

过滤器先后顺序:

1、注解配置:按照类名的字符串比较规则比较,值小的先执行

  如:AFilter和BFilter,AFilter先执行

2、web.xml配置:谁定义在上面谁先执行

复制代码
    <filter>
        <filter-name>filedemo1</filter-name>
        <filter-class>com.tomcat1.web.filter.FilterDemo7</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filedemo1</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>filedemo2</filter-name>
        <filter-class>com.tomcat1.web.filter.FilterDemo6</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filedemo2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
复制代码

 

 

posted @   xjw12345  阅读(1290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示