• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use of writing servlet-mapping tag in web.xml

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, I don't think so.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic