Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of writing servlet-mapping tag in web.xml

 
aman hindustani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all..

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

<servlet>
<servlet-name>serv</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>serv</servlet-name>
<url-pattern>/testurl1</url-pattern>
</servlet-mapping>

other than aliasing our servlet class..

Thanks..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
aman hindustani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i know that ....instead of our servelt class ..we are using..url pattern..is there any other use..other than that..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, I don't think so.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://faq.javaranch.com/view?InvokerServlet
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

<name>Ohio</name><param>5%</param>
<name>California</name><param>8%</param>

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!

-Cameron
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic