Printable Version Printable Version

Know more about Servlet Mapping

Sep 11th, 2009 | By admin | Category: Servlets

A web server takes a request from the client and send the response back to the client. A servlet is a web application component that can talk to web server through the container it’s deployed into. We know , we need to do servlet mapping into web.xml to define the url pattern to access the servlet. The servlet is the place where a service is processed. So it’s mandatory to define the URL pattern correctly into web.xml to access the servlet correctly.
We define a simple servlet mapping in the following way into web.xml file ,

Listing 1 : Servlet Mapping

<web-app xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”
version=”2.4”>
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>com.etechGuide.CustomerDetailsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/getCustomer</url-pattern>
</servlet-mapping>
</web-app>

The URL to access the CustomerServlet should be something like this,
http://YOUR_SERVER_IP:SERVER_PORT/CONTEXT_ROOT/getCustomer
(example : http://localhost:8080/customerdeal/getCustomer) .
Have a look at the above URL to access the servlet. We call the servlet by it’s url-pattern defined into the web.xml , not by it’s servlet-name. So what is the use of the tag? This is for use in other part of the web.xml. A client request a servlet.Requesting a servlet to get the servlet instantiated is done in the following way,

1. Request come to the web server in the form of a URL(http://localhost:8080/customerdeal/getCustomer)
2. Container look at the URL and find the possible url-pattern (in this case it is , getCustomer) in the URL.
3. Container look for the url-pattern inside any servlet-mapping tag into the web.xml file.
4. Once the url-pattern found , container locates the servlet-name tag , inside servlet-mapping tag(servlet name is CustomerServlet in the Listing 1).
5. Once the servlet-name is located , container search for the same servlet-name under a servlet tag.
6. If found , container locates the servlet-class tag element associated with the servlet-name tag it found in the above step.
7. If the servlet class (it is com.etechGuide.CustomerDetailsServlet in the Listing 1) is found , container instantiate the servlet.

So , now we know the importance of the servlet-name in servlet mapping. Servlet name is something client should not bother about but a web developer should map it carefully.
Lets have a look at the url-mapping tag closely. In the above example, url-pattern is getCustomer. Question is , is getCustomer is a resource in the server ? What does the client to do with url-pattern ? Does he really need to bother about it ? No. It is not a resource and client does not have to bother about the meaning of the url-pattern. The URL should have url-pattern in any of the following forms ,
EXACT Match URL Pattern :

<url-pattern>/Customer/getCustomer.do</url-pattern>

This type of pattern must begin with a slash(/) and might have or not have an extension(.do).

EXTENSION Match URL Pattern :

<url-pattern>*.do</url-pattern>

This type of pattern must not begin with a slash(/) but must begin with a asterik(*). There must be an extension(do , com etc) specified after the period(.).

DIRECTORY Match URL Pattern :

<url-pattern>/Customer/*</url-pattern>

This kind of pattern must start with a slash(/) followed by a real or virtual directory name. It must end with a slash(/) and an asterik(*).

  • Share/Save/Bookmark
Tags: ,

One comment
Leave a comment »

  1. nice

Leave Comment