The newfangled @WebFilter annotation is purty nice, but how can you specify the filter chain order?
In the good ole times it was specified by the order of appearance of the <filter-mapping> entries in web.xml, but how do do you do it now?
when using annotations to define the listeners, servlets and filters, the order in which they are invoked is unspecified.
If you want to define an order then you must use deployment descriptor.
Joined: Jan 17, 2008
Thanks, Piyush! That was as I feared. So I recon that the Servlet 3 @WebFilter annotation is actually a step backwards. Having the filter mappings (and order!) collected in one place is, after all, the Right Thing.
There is no one "Right Thing". Use the method that's most suitable to each situation. In many cases, filter order is irrelevant. When it is, use the deployment descriptor. When it's not, use either mechanism.
I would guess that in many cases the order is relevant.
For example, there is (to my knowledge) no way to change the default character encoding (ISO 8859-1) in a JEE webapp. If you want to be able to handle national characters, you need UTF-8. So you need a CharEncodingFilter that sets the char encoding to UTF-8 on all requests and responses, and that filter has to be first in the chain.
That would be the majority of all webapps, as far as I can tell.
In any case, everyone's needs are different and having a number of different ways to accomplish something means that anyone can choose the mechanism that works best for them and their current situation. It's rather myopic to always assume that what's best for you is always best for everyone else.