If we define webapp specific servlet filters in WAR’s own 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 those filters using @WebFilter 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
@WebFilterannotation. However, to minimize theweb.xmlusage, it’s sufficient to annotate all filters with just afilterNameso that you don’t need the<filter>definition, but just a<filter-mapping>definition in the desired order.For example,
with in
web.xmljust this:If you’d like to keep the URL pattern in
@WebFilter, then you can just do like so,but you should still keep the
<url-pattern>inweb.xml, because it’s required as per XSD, although it can be empty: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