aspose file tools*
The moose likes Servlets and the fly likes How specify filter chain with @WebFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How specify filter chain with @WebFilter" Watch "How specify filter chain with @WebFilter" New topic
Author

How specify filter chain with @WebFilter

Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
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

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How specify filter chain with @WebFilter