首页 > 其他分享 >过滤器拦截路径配置以及过滤器拦截方式配置和过滤器链

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

时间:2022-08-18 14:25:38浏览次数:45  
标签:index void 配置 jsp 过滤器 拦截 public

过滤器拦截路径配置

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>

 

 

标签:index,void,配置,jsp,过滤器,拦截,public
From: https://www.cnblogs.com/xjw12345/p/16598072.html

相关文章

  • swagger关于参数的配置
    1.springboot集成swagger2时get方式传参在swagger-ui.html中相应的api中为参数赋值问题在@ApilmplicitParam配置一个参数paramType="query"加在后面就行了。这个属性是......
  • Filter细节过滤器拦截路径配置、Filter细节过滤器拦截方式配置、Filter细节过滤器链(
    Filter细节过滤器拦截路径配置1.具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行2.拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行3.后......
  • Nginx_安装配置后,浏览器不能访问配置的站点ip:防火墙
    一、检查nginx安装成功[root@localhostsoftRyc]#/usr/local/webserver/nginx/sbin/nginx-vnginxversion:nginx/1.21.2 二、配置检查成功[root@localhostconf]#......
  • stylelint 配置使用,自动修复css,书写顺序
    stylelint配置使用,自动修复css,书写顺序https://juejin.cn/post/6940127032932040735阿离王lv-42021年03月16日13:58·阅读5100关注stylelint配置使用安装st......
  • vscode配置
    把编辑器从pycharm换成了vscode,倒不是因为后者更好用,相反我觉得前者更好用。而是因为一款插件clicknium,它只支持vscode。 1.快捷键配置//将键绑定放在此文件中......
  • 从零开始配置 vim(16)——启动界面配置
    不知道各位小伙伴用过spacevim或者LunarVim又或者doomvim或者其他的什么vim配置框架,我们发现他们的启动界面都做的比较好看,而我们默认进入的启动界面则显得比较素......
  • VSCode运行C/C++配置
    将MinGw安装目录下的1、安装 VSCode2、安装 MinGW链接:点击跳转3、MinGW 内安装两个模块1.右键MarkforInstallation勾选(此处已安装好,所以是绿色实心)2.......
  • 【Springboot】拦截器
    Springboot拦截器1.什么是拦截器?拦截器可以根据URL对请求进行拦截,主要应用于登陆校验、权限验证、乱码解决、性能监控和异常处理等功能。2.定义拦截器步骤在Spring......
  • Vue项目 invalid host header 问题 配置 disableHostCheck:true报错
    项目场景:解决Vue项目invalidhostheader问题disableHostCheck:true报错问题描述使用内网穿透时出现invalidhostheader找了好多都是让配置vue.config.js系统......
  • Linux下搭建ZooKeeper集群并整合Dubbo配置
    1.环境说明Zookeeper不仅可以单机提供服务,同时也支持多机组成集群来提供服务,实际上Zookeeper还支持另外一种伪集群的方式,也就是可以在一台物理机上运行多个Zookeeper......