This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Servlets and the fly likes use of writing servlet-mapping tag in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Servlets
Bookmark "use of writing servlet-mapping tag in web.xml" Watch "use of writing servlet-mapping tag in web.xml" New topic

use of writing servlet-mapping tag in web.xml

aman hindustani
Ranch Hand

Joined: Jun 15, 2006
Posts: 57
Hello to all..

Kindly any one explain clearly what is the use of writing <servlet-mapping> tag..



other than aliasing our servlet class..

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

When you make a request, you want your servlet to be called when a particular url is accessed by the user. Here, you are saying that accessing the "/testurl1" url will call the servlet named "serv".

[My Blog]
All roads lead to JavaRanch
aman hindustani
Ranch Hand

Joined: Jun 15, 2006
Posts: 57
yeah i know that ....instead of our servelt class ..we are using..url there any other use..other than that..
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

no, I don't think so.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Security is a mojor reason(the exact servlet class is not exposed).Makes a servlet called at different contexts using different mapping and that the deployer of the servlet can decide.

Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Your servlet mapping defines the url that will be used to access your servlet.

With traditional webserver (and most web scripting languages) the url is defined by the filename and location of the page within the server file system. Since servlets are not tied to the server's filesystem, (They could be in jars, wars, under the app's WEB-INF/classes directory or under directories common to all apps in the container), the servlet developer needs a way to specify how he/she wants the end user to invoke them.

For a while containers provided a way to invoke servlets by package/classname but it turned out to poor idea and was, in a sense, deprecated. See:
for more details.

De-coupling the URI from the layout of the application's file structure adds flexibility and security to Java webapps.

By the way: If you have a lot of servlets and consider creating a mapping for each one to be cumbersom, you might want to look into the "Front Controller" pattern. With it, you only need one mapping for your whole app.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

One thing you're missing is parameterization!

Parameters can be associated with a ServletMapping, not the Servlet Code.

I could have a class called com.examscam.servlet.StateTaxServlet

I could then map it 50 different ways:


Okay, bad xml, but you get the point. One Servlet code could result int 50 different servlets, all with different behavior based on the mapping.

That's a big deal!

Have you checked out Aspose?
subject: use of writing servlet-mapping tag in web.xml
It's not a secret anymore!