If we define Servlet filters in web.xml, then the order of execution of the filters will be the same as the order in which they are defined in the web.xml.

But, if we define the filters using annotation, what is the order of execution of filters and how can we determine the order of execution?

You can indeed not define the filter execution order using @WebFilter annotation. However, to minimize the web.xml usage, it's sufficient to annotate all filters with just a filterName so that you don't need the <filter> definition, but just a <filter-mapping> definition in the desired order.

For example,

@WebFilter(filterName="filter1")publicclassFilter1implementsFilter{}@WebFilter(filterName="filter2")publicclassFilter2implementsFilter{}

with in web.xml just this:

<filter-mapping><filter-name>filter1</filter-name><url-pattern>/url1/*</url-pattern></filter-mapping><filter-mapping><filter-name>filter2</filter-name><url-pattern>/url2/*</url-pattern></filter-mapping>

If you'd like to keep the URL pattern in @WebFilter, then you can just do like so,

@WebFilter(filterName="filter1", urlPatterns="/url1/*")publicclassFilter1implementsFilter{}@WebFilter(filterName="filter2", urlPatterns="/url2/*")publicclassFilter2implementsFilter{}

but you should still keep the <url-pattern> in web.xml, because it's required as per XSD, although it can be empty:

<filter-mapping><filter-name>filter1</filter-name><url-pattern/></filter-mapping><filter-mapping><filter-name>filter2</filter-name><url-pattern/></filter-mapping>

Regardless of the approach, this all will fail in Tomcat until version 7.0.28 because it chokes on presence of <filter-mapping> without <filter>. See also Using Tomcat, @WebFilter doesn't work with <filter-mapping> inside web.xml

How to define Servlet filter order of execution using annotations的更多相关文章

  1. java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter

    java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast ...

  2. java.lang.ClassNotFoundException: javax.servlet.Filter

    java.lang.ClassNotFoundException: javax.servlet.Filter:有两个原因:(1)在maven中的作用域,不能是provided,需要是compile就是 ...

  3. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  4. Spring Boot 2 实战:如何自定义 Servlet Filter

    1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter 来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等 ...

  5. Spring Boot 实战:如何自定义 Servlet Filter

    1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等. ...

  6. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  7. java Servlet Filter 拦截Ajax请求

    /** * 版权:Copyright 2016-2016 AudaqueTech. Co. Ltd. All Rights Reserved. * 描述: * 创建人:赵巍 * 创建时间:2016年1 ...

  8. Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

    使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...

  9. 理解Servlet过滤器 (javax.servlet.Filter)

    过滤器(Filter)的概念 过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应. 在请求到达Servlet/JSP之前,过滤器截获请求. 在响应送给客户端之前,过滤器截获响 ...

随机推荐

  1. 银联接口测试——详细(JAVA)

    准备材料 1.注册账号 https://open.unionpay.com/ajweb/register?locale=zh_CN 2.▼登录账号 -->帮助中心--> 下载,选择网关支付 ...

  2. android ListView的怪异现象

    我们已经知道,当条目没显示一次,那个类重写的最后一个函数就执行一次,但是现在,发生了怪异现象!当窗体的属性设置为包裹的时候,会重复显示多次,所以,高度,宽度都要设置为充满类型才可以

  3. 【风马一族_Python】 实施kNN算法

    一.在PyCharm 5.0.4(编写python程序的IDE) 编写kNN.py文件的代码 -------------------------- 1. kNN.py  运算符模块 --------- ...

  4. 版本控制器 (Svn,Git)

    Svn: 集中式版本控制器,首先开发者在开始新一天的工作之前必须从服务器获取代码,然后进入自己的分支开发,开发完成后把自己的分支合并到主分支上进行提交,解决冲突.所有的版本信息都放在服务器上.如果脱离 ...

  5. js获取location.href的参数实例代码

    本文为大家介绍下js如何获取location.href的参数,需要注意的是去掉参数里最开头的?号,具体实现如下,有需要的朋友可以参考下,希望对大家有所帮助 window.location.search ...

  6. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  7. 【转载】mysqldump的single-transaction和master-data

    原文地址:mysqldump的single-transaction和master-data 作者:myownstars 先看一下--lock-tables和--lock-all-tables --lo ...

  8. 12、在XAML中定义处理程序

    <Grid> <Button x:Name="btnTest" Width="120" Height="36" Conte ...

  9. ubuntu 12.04版本出现界面终端打开broken pipe,但是tty1这些可以。

    sudo apt-get remove xserver-xorg sudo apt-get install xserver-xorg

  10. .net视图中日期格式化

    昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...