Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify order of filter mappings on GlassFish?

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

like image 354
rapadura Avatar asked Feb 08 '11 11:02

rapadura


2 Answers

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.

like image 54
axtavt Avatar answered Oct 11 '22 01:10

axtavt


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:

  1. 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.

  2. 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.

  3. 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.

like image 33
Stephen C Avatar answered Oct 11 '22 00:10

Stephen C