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.
Joined: Jun 01, 2011
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.