rapadura
rapadura

Reputation: 5300

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

Upvotes: 7

Views: 1074

Answers (2)

Stephen C
Stephen C

Reputation: 719689

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.

Upvotes: 2

axtavt
axtavt

Reputation: 242786

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.

Upvotes: 8

Related Questions