what are the rules according to which path given in
<url-pattern>
of
<servlet-mapping>
tag is mapped to servlets?
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.
<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 :
Reference : How to configure Servlet.
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.
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:
- 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.
- 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.
- 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.
- 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.
<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.
/
,/*
? – Aniket Kulkarni