| Author |
use of writing servlet-mapping tag in web.xml
|
aman hindustani
Ranch Hand
Joined: Jun 15, 2006
Posts: 53
|
|
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
Joined: Nov 24, 2005
Posts: 14685
|
|
|
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: 53
|
|
|
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
Joined: Nov 24, 2005
Posts: 14685
|
|
|
no, I don't think so.
|
 |
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
|
|
|
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
Sheriff
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: 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.
|
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: 4967
|
|
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
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
 |
|
|
subject: use of writing servlet-mapping tag in web.xml
|
|
|