I read that the order in which filters are processed can be determined by the order in which they are declared in web.xml
But how to do this without web.xml, using for example the @WebServlet annotation? I dont want to clutter my web.xml
Looks like it's impossible for annotated filters. Servlet 3.0 Specification says:
As described above, when using annotations to define the listeners, servlets and filters, the order in which they are invoked is unspecified.
As @axtavt notes, you cannot do it. Here's why (I think) they designed it that way.
To specify the order, the annotations would need an extra argument that (somehow) specifies the position in the chain; e.g. an 'order'. There are problems with this:
If a servlet has a number of filters whose order is specified by annotation parameters, then the programmer / deployer has to examine the annotations for all of the filter classes to work out what the actual order is.
A given filter class can in theory be used in multiple servlets, even multiple webapps. Each servlet or webapp may want the filter order to be different. You cannot achieve this simply using an annotation on the filter class.
If someone deploying a webapp needed to alter the filter order, he/she would need to modify the source code, recompile and rebuild the WAR file.
I imagine the designers looked at these problems and decided that the best place to specify the filter order is in the web.xml file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With