Tuesday, April 27, 2010

What is servlet mapping?

What is servlet mapping?


Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.
How is servlet mapping defined?

Servlets should be registered with servlet container. For that, you should add entries in web deployment descriptor web.xml. It is located in WEB-INF directory of the web application.
Entries to be done in web.xml for servlet-mapping:

<-servlet-mapping>
<-servlet-name>milk<-/servlet-name>
<-url-pattern>/drink/*<-/url-pattern>
<-/servlet-mapping>

servlet-mapping has two child tags, url-pattern and servlet-name. url-pattern specifies the type of urls for which, the servlet given in servlet-name should be called. Be aware that, the container will use case-sensitive for string comparisons for servlet matching.
Syntax for servlet mapping as per servlet specification SRV.11.2:

A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
A string containing only the ‘/’ character indicates the “default” servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.

Rule for URL path mapping:

It is used in the following order. First successful match is used with no further attempts.

1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a “default” servlet is defined for the application, it will be used.

What is implicit mapping?

A servlet container can have a internal JSP container. In such case, *.jsp extension is mapped to the internal container. This mapping is called implicit mapping. This implicit mapping allows ondemand execution of JSP pages. Servlt mapping defined in web application has high precedence over the implicit mapping.
Example code for java servlet mapping:

<-servlet>
<-servlet-name>milk<-/servlet-name>
<-servlet-class>com.javapapers.Milk<-/servlet-class>
<-/servlet>
<-servlet>
<-servlet-name>points<-/servlet-name>
<-servlet-class>com.javapapers.Points<-/servlet-class>
<-/servlet>
<-servlet>
<-servlet-name>controller<-/servlet-name>
<-servlet-class>com.javapapers.ControllerServlet<-/servlet-class>
<-/servlet>

<-servlet-mapping>
<-servlet-name>milk<-/servlet-name>
<-url-pattern>/drink/*<-/url-pattern>
<-/servlet-mapping>
<-servlet-mapping>
<-servlet-name>points<-/servlet-name>
<-url-pattern>/pointlist<-/url-pattern>
<-/servlet-mapping>
<-servlet-mapping>
<-servlet-name>controller<-/servlet-name>
<-url-pattern>*.do<-/url-pattern>
<-/servlet-mapping>

No comments: