How to define Servlet filter order of execution using annotations

时间:2022-10-20 16:01:49

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&period;lang&period;ClassCastException&colon; org&period;springframework&period;web&period;filter&period;CharacterEncodingFilter cannot be cast to javax&period;servlet&period;Filter

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

  2. java&period;lang&period;ClassNotFoundException&colon; javax&period;servlet&period;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&sol;filter&sol;listener&sol;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&period;servlet&period;Filter 报错&comma; 原因servlet-api&period;jar冲突

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

  9. 理解Servlet过滤器 &lpar;javax&period;servlet&period;Filter&rpar;

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

随机推荐

  1. javascript学习内容--object&period;style&period;display&equals;&quot&semi;value&quot&semi; value值为&OpenCurlyDoubleQuote;”none&OpenCurlyDoubleQuote;隐藏”或 &quot&semi;block&quot&semi;显示

    <head> var mychar=document.getElementById("con"); function hidden(){ mychar.style.di ...

  2. 安装R语言扩展包diveRsity-1

    今天去了学院的运动会呢-扮熊本熊超开心-写完这篇我补上我的图么么哒 ××××××××××××文末高能预警!!!!!这个包的安装并不是本周的任务!!!!!我真是萌萌哒×××××××××××××× ××× ...

  3. html--第一章 基础知识总结

    1--<body bgcolor="red">背景颜色 2--<body backgroud="back-ground.gif">  背 ...

  4. Qt之自定义控件(开关按钮)

    简述 接触过IOS系统的童鞋们应该对开关按钮很熟悉,在设置里面经常遇到,切换时候的滑动效果比较帅气. 通常说的开关按钮,有两个状态:on.off. 下面,我们利用自定义控件来实现一个开关按钮. 简述 ...

  5. 1、ViewModel类的构建和INoyifyPropertyChanged的应用

    public class SampleItem : INotifyPropertyChanged { public SampleItem() { } private string title; pub ...

  6. Android学习笔记⑦——UI组件的学习AdapterView相关1

    AdapterView是一个非常重要的组件之一,他非常灵活,所以得好好学...AdapterView本身是一个抽象类,派生出来的子类用法也十分相似,只是界面有一定的区别,因此本节把他们归为一类 Ada ...

  7. 【BZOJ】【1927】【SDOI2010】星际竞速

    网络流/费用流 比较简单的一题,对于每个星球,将它拆成两个点,然后二分图建模:左部结点与S相连,流量为1费用为0:右部结点与T相连,流量为1费用为0:对于每条航道x->y,连边x->y+n ...

  8. ASP&period;NET多次点击提交按钮以及Session超时和丢失过期问题

    1.ASP.NET防止多次点击提交按钮 对于一个按钮,要让变成恢色的,只要this.disabled=true就可以了,可是在.NET里,添加了OnClick事件后,就无法提交信息了.所以要加上以下代 ...

  9. 史上最全CentOS安装教程,图文结合

    这是我最近整理的一份最全的CentOS安装步骤,亲自测试步骤,步步都有截图,步骤清晰.按此教程可轻松装机,并且安装成功的主机能访问外部网络. 闲话不说,首先介绍一下本教程用到工具: VMware Wo ...

  10. 统计输入的汉字,数字,英文,other数量

    主要用正则表达式在完成对汉字,数字,英文数量的验证. import java.util.Scanner; /* * 统计汉字,数字,英文,other * */ public class Test { ...