189

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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
siva636
  • 16,109
  • 23
  • 97
  • 135

4 Answers4

205

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")
public class Filter1 implements Filter {}

@WebFilter(filterName="filter2")
public class Filter2 implements Filter {}

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/*")
public class Filter1 implements Filter {}

@WebFilter(filterName="filter2", urlPatterns="/url2/*")
public class Filter2 implements Filter {}

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 7
    they could've introduced an `order` attribute of a nested `@WebFilterMapping` annotation. I wonder whether the didn't do it for simplicity – Bozho Oct 29 '11 at 09:39
  • 13
    @Bozho: That would not be specific enough. What if your webapp ships with 3rd party libraries which includes a filter? It's hard to tell its order beforehand. – BalusC Oct 29 '11 at 19:07
  • 1
    @BalusC: Something went wrong in your example: url-pattern is closed with a filter-name. – AndrewBourgeois May 29 '12 at 21:32
  • 3
    @AndrewBourgeois: Fixed. Was an copypaste error. Too bad that Markdown editor doesn't have builtin XML validation like as in Eclipse ;) – BalusC May 29 '12 at 21:33
  • @BalusC: In the end this doesn't work for me, see this question: http://stackoverflow.com/questions/10856866/webfilter-doesnt-work-with-ordering-in-web-xml-in-tomcat-only – AndrewBourgeois Jun 03 '12 at 18:19
  • An annotation similar to `@DependsOn` for EJB would be great. I am not 100% sure but in glassfish with mojarra the url-pattern in xml can be left empty so that the patterns can still be defined via annotation. – djmj Aug 06 '13 at 19:54
  • 1
    @Craig: you apparently never tried his answer. His answer **does not** work at all. The `` has a completely different purpose. It's completely beyond me why it's currently on 12 votes. – BalusC Aug 07 '13 at 11:06
  • @djmj: that would be illegal syntax according XSD of `web.xml`. – BalusC Aug 07 '13 at 11:08
  • I meant empty not leaving it. – djmj Aug 07 '13 at 12:19
  • 8
    Using `` doesn't work on JBoss EAP 6.1 - it overrides the `@WebFilter` value and prevents the filter from running at all. – seanf Oct 21 '13 at 05:11
  • @BalusC has anything changed in tomcat 8? – Dejell Feb 18 '15 at 13:21
  • @BalusC what if I want to filter all url patterns except one? Eg. *I don't want to use filter for my `/welcome` url pattern.* – Yubaraj Jul 07 '16 at 15:54
  • I wish there was an easier way. Jersey seems to solve something like this in a smarter way via [javax.annotation.Priority](http://docs.oracle.com/javaee/7/api/javax/annotation/Priority.html). See [Jersey Filters and Interceptors documentation](https://jersey.java.net/documentation/latest/filters-and-interceptors.html) – nacho4d Sep 04 '16 at 06:50
  • It is not working in tomcat10. – eastwater Mar 21 '22 at 01:08
13

The Servlet 3.0 spec doesn't seem to provide a hint on how a container should order filters that have been declared via annotations. It is clear how about how to order filters via their declaration in the web.xml file, though.

Be safe. Use the web.xml file order filters that have interdependencies. Try to make your filters all order independent to minimize the need to use a web.xml file.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • 4
    I have many Servlet filters in my project, out of them only a particular filter must be called first and order of other filters not a concern. Do I have to defile all filters in web.xml? Or are there any short-cuts? – siva636 Jul 03 '11 at 07:40
0

If Spring exists in your project you could use org.springframework.core.annotation.Order annotation over the class. F.e. @Order(0), @Order(1) etc.

-3
  1. Make the servlet filter implement the spring Ordered interface.
  2. Declare the servlet filter bean manually in configuration class.
    import org.springframework.core.Ordered;
    
    public class MyFilter implements Filter, Ordered {

        @Override
        public void init(FilterConfig filterConfig) {
            // do something
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            // do something
        }

        @Override
        public void destroy() {
            // do something
        }

        @Override
        public int getOrder() {
            return -100;
        }
    }


    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    @ComponentScan
    public class MyAutoConfiguration {

        @Bean
        public MyFilter myFilter() {
            return new MyFilter();
        }
    }