• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How specify filter chain with @WebFilter

 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With annotations order is unspecified.

From section 8.2.3 of Servlet 3.0 specification:
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.
 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not anything I've ever done.

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.

Choice is good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic