30

Suppose I have following in my web.xml

<filter-mapping>
    <filter-name>F1</filter-name>
    <url-pattern>/XYZ/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>F2</filter-name>
    <url-pattern>/XYZ/abc.do</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>F3</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

What will be the order in which the filters will be called if a request comes as /XYZ/abc.do and why?

Eric
  • 6,563
  • 5
  • 42
  • 66
Xyzxyz Xyz
  • 415
  • 1
  • 5
  • 6

2 Answers2

36

In the order their mappings are defined in web.xml

If using annotations (@WebFilter) the order seems to be undefined - you still have to declare the <filter-mapping> entries in web.xml.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • so its F1,F2,F3?What if in case of servlet? lets say i have 1 pattern matching 2 servlets? – Xyzxyz Xyz Oct 29 '11 at 09:39
  • 1
    it doesn't matter how much servlets/uris it matches. The current target resource is only one, and for it, the filters are invoked in the order of the mapping declaration. (So yes - F1,F2,F3) – Bozho Oct 29 '11 at 09:41
  • yes i got it.But now i am asking abt a totally seperate scenerio where same url patter matches to 2 different servlet s1 and s2.Which servlet will be called?This quesiton is independent of filters – Xyzxyz Xyz Oct 29 '11 at 09:44
  • if it's independent, ask another question ;) (and mark an answer as accepted on this one) – Bozho Oct 29 '11 at 09:45
  • ok done............................................... :).Can u comment now? – Xyzxyz Xyz Oct 29 '11 at 09:47
  • by "ask another question" I meant to press the "Ask question" button and formulate the new question – Bozho Oct 29 '11 at 09:48
  • done http://stackoverflow.com/questions/7938138/what-if-url-pattern-matches-multiple-servlets – Xyzxyz Xyz Oct 29 '11 at 09:51
29

Section 6.2.4 of the Servlet specification 3.0:

When processing a <filter-mapping> element using the <url-pattern> style, the container must determine whether the <url-pattern> matches the request URI using the path mapping rules defined in Chapter 12, “Mapping Requests to Servlets”.

The order the container uses in building the chain of filters to be applied for a particular request URI is as follows:

  1. First, the <url-pattern> matching filter mappings in the same order that these elements appear in the deployment descriptor.

  2. Next, the <servlet-name> matching filter mappings in the same order that these elements appear in the deployment descriptor.

If a filter mapping contains both <servlet-name> and <url-pattern>, the container must expand the filter mapping into multiple filter mappings (one for each <servlet-name> and <url-pattern>), preserving the order of the <servlet-name> and <url-pattern> elements.

In short: they're applied in the order in which they appear in the XML file. It gets interesting if you hit an URL that's covered by both <url-pattern> and <servlet-name> bound filters, because then all URL-pattern bound filters are applied before all servlet-name bound filters. I've never been in this situation (haven't seen any servlet-name bound filters at all), but I reckon it could be quite confusing.

Community
  • 1
  • 1
Barend
  • 17,296
  • 2
  • 61
  • 80
  • Thank you for the hint. I had `` and `` bound filters in my `web.xml` and was wondering why the URL-pattern bound filter was executed first, even if the servlet-name bound filter was defined first in the XML. – Sky Nov 11 '21 at 14:43