You can map a servlet to more than one paths so if the mapping is separate, you can decide url matching more flexibly by ordering the servlet-mapping declaration in the order you want the servlets to match an incoming request. Something like this
Interesting question. Not sure why the Sun engineers designed it this way, but they could have put the <url-pattern> right inside the <servlet> tag, couldn't they? I don't see a reason why theoretically that wouldn't work. Allowing multiple <url-pattern> tags could give the flexibility of mapping different kinds of urls to the same servlet, that Ankit is talking about.
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Probably, from maintainability viewpoint, they kept <url-pattern> tag separate from <servlet>. If its a part of <servlet>, imagine what you'd need to do if you later want a url path to be handled by a different servlet (existing or a new one). You'd have to delete the <url-pattern> from the current <servlet> and make a new entry in the other <servlet>. Instead, keeping this tag in a separate <servlet-mapping> tag gives you the ease of maintenance by being able to change just the name of the servlet without touching other parts in the descriptor.
We don't have time to be charming! Quick, read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database