0
votes

what are the rules according to which path given in

<url-pattern>

of

<servlet-mapping>

tag is mapped to servlets?

5
You want to know how does mapping happen? Or What are difference between different url patterns such as /, /* ?Aniket Kulkarni
I want to know how mapping happens.a Learner

5 Answers

1
votes

i will explain you with following sample example code.

 <servlet>
   <description></description>            // enter description of servlet
   <display-name>GetCascadParamServlet</display-name>  // the name which display in url
   <servlet-name>GetCascadParamServlet</servlet-name>   // Actual servlet name
   <servlet-class>com.agileinfotech.bsviewer.servlet.GetCascadParamServlet</servlet-class> // this is package name , where exactly your servlet locate 
 </servlet>
 <servlet-mapping>
   <servlet-name>GetCascadParamServlet</servlet-name>  // servlet name which exact declare same as your servlet name
   <url-pattern>/GetCascadParamServlet</url-pattern>  // if we want to call another servlet or class we can declare it here when it will see that url redirect it to the that configuration.
 </servlet-mapping>

Hope that you will understood the whole mapping of servlet.

1
votes

<url-pattern> specifies the type of urls for which, the servlet given in servlet-name should be called.As per servlet specification SRV.11.2 for string or path in url pattern :

  1. A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
  2. A string beginning with a ‘*.’ prefix is used as an extension mapping.
  3. 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.
  4. All other strings are used for exact matches only.

Reference : How to configure Servlet.

0
votes

In lay man's terms, what you specify in url pattern, will be redirected to the servlet specified. for example:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>com.blah.blah.ActionDispatcher</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.z</url-pattern>
</servlet-mapping>

so when you url is localhost:8080/app/test.z the ActionDispatcher servlet will handle it.

0
votes

How does Web container map client requests to servlets?

Upon receipt of a client request, the Web container determines the Web application to which to forward it. The Web application selected must have the the longest context path that matches the start of the request URL. The matched part of the URL is the context path when mapping to servlets.

The Web container next must locate the servlet to process the request using the path mapping procedure described below.

The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters. The URL path mapping rules below are used in order. The first successful match is used with no further matches attempted:

  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. The container must use case-sensitive string comparisons for matching.

Source

0
votes
<servlet>
<servlet-name>WAP Callback Servlet</servlet-name>
<servlet-class>
com.ism.integeration.WAPBilldeskCallbackServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WAP Callback Servlet</servlet-name>
<url-pattern>/jsp/wapcallback/*</url-pattern>
</servlet-mapping>

when any response url which include "wapcallback" it will foreword to corresponding servlet.