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. 【NPOI】.NET EXCEL导入导出开发包

    1.导出 //工作簿HSSFWorkbook HSSFWorkbook hssfworkbook = new HSSFWorkbook(); //ISheet页 ISheet sheet1 = hss ...

  2. FPGA统计摄像头输出-基于MD9T112

    FPGA HDL源程序 FPGA统计摄像头的输出像素,窗口尺寸等等 //---------------------------------------------------------------- ...

  3. c#枚举自定义,用于数据绑定。

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum)] public ...

  4. C#定时器和事件

    C#定时器和事件 System.Timers.Timer myTimer; private void Form1_Load(object sender, EventArgs e) { myTimer ...

  5. Jexus 高并发请求的优化技巧 笔记

    Jexus web server 5.1 每个工作进程的最大并发数固定为1万,最多可以同时开启4个工作进程,因此,每台Jexus V5.1服务器最多可以到支持4万个并发连接.但是,按照linux系统的 ...

  6. apache 多站点搭建

    一.apache配置多站点方法一 1.首先修改apache httpd.conf 文件 启用虚拟主机组件功能 取消 LoadModule vhost_alias_module modules/mod_ ...

  7. python 安装 setuptools Compression requires the (missing) zlib module 的解决方案

    背景: 虚拟机centos下安装python辅助工具 setuptools报错,错误信息大概如下: Traceback (most recent call last): File "setu ...

  8. mysql基本知识---20151127-1

    2015年11月27日,作为PHPer的我开始全面学习mysql数据库. 基本语法: 1.连接服务器: mysql>mysql -h host -u root -p 回车 输入密码(本地环境可以 ...

  9. c#中的结构与枚举

    结构 与c++不同的是,结构应该定义在命名空间或者类里面,成员变量叫字段,字段并且有访问控制符,每个字段前要加一个下划线 例子 using System; using System.Collectio ...

  10. ActiveMQ之deliveryMode

    在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS. import javax.jms.Connection;import javax.jms.De ...