Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding filter invocation

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Filters are invoked in the order of their registration BUT is it not wrong to
say that filter are called in the order they appear in th DD ( bec' filters with <url-pattern>
are invoked before filter with <servlet-name>) ?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2415
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simran,

It is a bit more subtle: if there are let's say two filters defined on the same servlet: then they will be invoked in the same order of how they are ordered in the deployment descriptor (web.xml). But that requirement comes after the fact of <url-pattern> before <servlet-name>

See the servlet spec explanation:
SRV.6.2.4 Configuration of Filters in a Web Application
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.

This requirement means that the container, when receiving an incoming request, processes the request as follows:
- Identifies the target Web resource according to the rules of "Specification of Mappings" on page 86.
- If there are filters matched by servlet name and the Web resource has a
<servlet-name>, the container builds the chain of filters matching in the order
declared in the deployment descriptor. The last filter in this chain corresponds
to the last <servlet-name> matching filter and is the filter that invokes the target
Web resource.
- If there are filters using <url-pattern> matching and the <url-pattern>
matches the request URI according to the rules of Section SRV.11.2, "Specification
of Mappings", the container builds the chain of <url-pattern> matched
filters in the same order as declared in the deployment descriptor. The last filter
in this chain is the last <url-pattern> matching filter in the deployment descriptor
for this request URI. The last filter in this chain is the filter that invokes
the first filter in the <servlet-name> matching chain, or invokes the target Web
resource if there are none.

Regards,
Frits
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exactly.

But the problem is I found a the foll Q in M.Green-


Filters are called in the order they appear in the DD.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simran, that option in Marcus' exam is ambiguous. Filters are called in the order they appear in web.xml but its like a two time pass. First filters are invoked based on url-pattern and then servlet-name. So for the real exam the statement would be false...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic