How to define Servlet filter order of execution using annotations
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的更多相关文章
- 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 ...
- java.lang.ClassNotFoundException: javax.servlet.Filter
java.lang.ClassNotFoundException: javax.servlet.Filter:有两个原因:(1)在maven中的作用域,不能是provided,需要是compile就是 ...
- Spring boot中使用servlet filter
Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...
- Spring Boot 2 实战:如何自定义 Servlet Filter
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter 来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等 ...
- Spring Boot 实战:如何自定义 Servlet Filter
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等. ...
- servlet/filter/listener/interceptor区别与联系
转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...
- java Servlet Filter 拦截Ajax请求
/** * 版权:Copyright 2016-2016 AudaqueTech. Co. Ltd. All Rights Reserved. * 描述: * 创建人:赵巍 * 创建时间:2016年1 ...
- 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 ...
- 理解Servlet过滤器 (javax.servlet.Filter)
过滤器(Filter)的概念 过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应. 在请求到达Servlet/JSP之前,过滤器截获请求. 在响应送给客户端之前,过滤器截获响 ...
随机推荐
- 打造简单实用的Thinkphp分页样式(Bootstrap版本)
先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...
- jquery 1.6发布后,增加prop()方法部分取代attr()方法
以前的jq中,全部使用attr来访问对象的属性,比如取一个图片的alt属性,就可以这样做$('#img').attr('alt'); 但是在某些时候,比如访问checkbox的disabled属性 ...
- ORACLE 基础知识积累
创建ORACLE 数据库,首先用Sys账号角色为dba进入数据库然后,然后根据创建数据库的表空间,然后创建角色,创建完角色后将表空间的权限授予角色. SQL语句如下: create temporary ...
- 【Qt】QWidget、QDialog、QMainWindow的异同点【转】
简述 在分享所有基础知识之前,很有必要在这里介绍下常用的窗口-QWidget.QDialog.QMainWindow. 熟悉Qt的同学都应该知道,在新建Qt Widgets项目进行类信息选择时会碰到它 ...
- jeecms子栏目或者文章页导航父栏目选中解决方法
jeecms在子栏目或者文章页导航父栏目选中,看例子 <div class="nav"> <ul> [@cms_channel_list ] <li ...
- 每日一“酷”之array
array--国定类型数据序列 array模块定义一个序列数据结构,看起来和list非常相似,只不过所有成员都必须是相同的基本类型. 1.初始化 array实例化时可以提高一个参数来描述允许哪个种数据 ...
- jvm 数据区划分学习
Java virtual machine 运行时数据存储区域划分 2015年1月25日 19:15 Pc 寄存器 Each Java Virtual Machine thread has its o ...
- win2008修改最大远程桌面连接数
win2008修改最大远程桌面连接数 运行——gredit.msc——管理模板——windows组件——远程桌面服务——远程桌面回话主机——连接——限制连接的数量——修改为999999
- 修复jquery.treeview的增加子节点的方法的bug
1.修复理由 在一个android项目中用到了treeview控件(本来自己通过android的原生api实现了一个http://www.cnblogs.com/Mr-Nobody/p/3527688 ...
- java性能优化策略
1. 尽量使用局部变量代替成员变量,循环中对成员变量.方法的调用不超过2次 2. ArrayList如果知道大小,初始化时应指明 3. HashMap的遍历,用Entry 4. 如果确定类不可继承尽量 ...