File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Order of elements in DD and servlet/servlet-mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Order of elements in DD and servlet/servlet-mapping" Watch "Order of elements in DD and servlet/servlet-mapping" New topic

Order of elements in DD and servlet/servlet-mapping

Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Hello Rancheros,
I've read this article and it reports:
...For each servlet you want to call, provide a pair of tags like the following:



There are a couple things to note when you do this. Order of elements in web.xml is very important. So when you go to create your second 'couplet', make sure all your <servlet> tags are declared before your <servlet-mapping> tags. You'll maybe want to keep the 'couplets' together, because it will be easier to see which one belongs to which, but don't do it!...

Now I don't understand the two bold texts:
1) I know that the order of elements in deployment descriptor is not important, but here it tells the opposite.
2) Why is not advisable to keep <servlet> and <servlet-mapping> together?


No fun at the table, no fun in the sack.
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Hi Karim,

I didn't read the article but from what I see I think that they just tried to protect people that just start to learn about servlets from unexpected behavior.

1. You are right the order in DD doesn't matter in general. But this is not completely true. When you have many <servlet-mapping> elements that overlap(uri-patterns overlap) then the container has special conditions(rules) to determine which servlet to choose. You can read about this in "HF Servlets and JSP" book Chapter 11 page 619. Or even better in Serlvet Specification 3.0 - Chapter 12 Mapping Request to servlets(it is only 3 pages).

In general the rule is that the container orders mappings(I will provide the order above) and then starts choosing the servlet mapping that wins the competition - the first found mapping is used:
- Exact match <servlet-mapping>/hello/mello</servlet-mapping>
- Directory match <servlet-mapping>/hello/*</servlet-mapping>
- Extension match <servlet-mapping>*.jsp</servlet-mapping>

2. I suppose that if you keep all mappings in one place it will be easier to see which mapping will be chosen by the container. If you have mappings scattered all around the DD you might miss duplicate mapping and it will become nightmare finding the problem in your application.


I agree. Here's the link:
subject: Order of elements in DD and servlet/servlet-mapping
It's not a secret anymore!