How to determine the display order of filters on GlassFish?

I read that the processing order of filters 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, @WebServlet annotation? I don't want to clutter my web.xml

+6
java java-ee servlet-filters
source share
2 answers

This seems to be impossible for annotated filters. Servlet 3.0 Specification :

As described above, when using annotations to define listeners, servlets, and filters, the order in which they are invoked is not specified.

+7
source share

As @axtavt notes, you cannot do this. That's why (I think) they designed it that way.

To indicate order, annotations will require an additional argument that (somehow) determines the position in the chain; e.g. Order '. There are problems with this:

  • If the servlet has several filters, the order of which is specified by the annotation parameters, then the programmer / deployer should examine the annotations for all filter classes to determine what the actual order is.

  • This class of filters can theoretically be used in several servlets, even in several web folders. Each servlet or webapp may require that the order of the filters be different. You cannot achieve this simply by using annotation in the filter class.

  • If someone deploys the webapp necessary to change the filtering order, he will need to change the source code, recompile and rebuild the WAR file.

I think that the designers considered these problems and decided that the best place to specify the filtering order is in the web.xml file.

+2
source share

All Articles