What will be the order in which filters will be called? [duplicate]
Asked Answered
C

2

31

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?

Crappie answered 29/10, 2011 at 9:30 Comment(0)
E
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.

Expensive answered 29/10, 2011 at 9:33 Comment(7)
so its F1,F2,F3?What if in case of servlet? lets say i have 1 pattern matching 2 servlets?Crappie
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)Expensive
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 filtersCrappie
if it's independent, ask another question ;) (and mark an answer as accepted on this one)Expensive
ok done............................................... :).Can u comment now?Crappie
by "ask another question" I meant to press the "Ask question" button and formulate the new questionExpensive
done #7938638Crappie
A
30

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.

Age answered 29/10, 2011 at 9:45 Comment(1)
Thank you for the hint. I had <url-pattern> and <servlet-name> 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.Milquetoast

© 2022 - 2024 — McMap. All rights reserved.