Introduction to the Java EE: mapping configuration for Servlet-mapping

Source: Internet
Author: User

The <servlet-mapping> element defines a mapping between the servlet and the URL style. It contains two sub-elements <servlet-name> and <url-pattern>

The servlet name given by the <servlet-name> element must be the name of the servlet declared in the <servlet> element

The <url-pattern> element specifies the URL path that corresponds to the servlet, which is the path relative to the root of the Web application.

The Servlet 2.5 specification allows <servlet-mapping> 's <url-pattern> child elements to appear more than once, and the previous specification allows only one <servlet-mapping> element to contain a single < Url-pattern> the child element.

For example:

<servlet-mapping>

<servlet-name>helloworld</servlet-name>

<url-pattern>/hello</url-pattern>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

Let's look at the following example:

After configuring a mapping between a servlet and a URL style, when the servlet container receives a request, it first determines which Web application the request should respond to.

This is determined by comparing the starting part of the request URI with the context path of the Web application.

The path mapped to the servlet is the path to the request URI minus the context, and the Web application's context object, after removing the request URI's contextual path, processes the remaining part of the path in the order of the following path mapping rules, and after the first successful match is found, no longer Next match.

1. The container tries to exact match the path of the request and the path of the servlet map, and if the match succeeds, call the servlet to process the request.

2. The container tries to match the longest path prefix, with a slash (/) as the path delimiter, decrements the match by the path tree, and selects the longest matching servlet to process the request.

3. If the requested URL path finally has an extension, such as the. Jsp,servlet container, it tries to match the servlet that handles this extension.

If a matching servlet is not found by the previous 3 rules, the container invokes the Web application's default servlet to process the request, and if no default servlet is defined, the container sends an HTTP 404 error message to the client (the request resource does not exist).

In the deployment descriptor, you can use the following syntax to define the mappings.

A string that begins with/starts and ends with/* is used to map the path, for example:

<url-pattern>/admin/*</url-pattern>


to *. The prefix string is used to map the extension, for example: if there is no exact match, all requests for resources under the/admin/path will be handled by a servlet that maps the URL style described above.

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


With a separate/direct servlet for this Web application, for example: if there is no exact match and path match, then the request for a resource with a. Do extension will be handled by a servlet that maps the above URL style.

<url-pattern>/</url-pattern>


All other characters are used for exact matching, for example: if a matching servlet is not found for a request, the default servlet for the Web application is used for processing.

<url-pattern>/login</url-pattern>


Let's look at a few examples of request mappings, as shown in table 3-2 and table 3-3. If/login is requested, it is handled by a servlet that maps the URL-style/login.

Table 3-2 servlet Mappings

The mapped URL The corresponding servlet
/hello Servlet1
/bbs/admin/* Servlet2
/bbs/* Servlet3
*.jsp Servlet4
/ Servlet5

Table 3-3 Results of actual request mappings

Remove the remaining path of the context path Servlet that handles the request
/hello Servlet1
/bbs/admin/login Servlet2
/bbs/admin/index.jsp Servlet2
/bbs/display Servlet3
/bbs/index.jsp Servlet3
/bbs Servlet3
/index.jsp Servler4
/hello/index.jsp Servlet4
/hello/index.html Servlet5
/news Servlet5

Tomcat configures the default servlet in the%catalina_home%\conf\web.xml file with the following configuration code:


<servlet>
  <servlet-name>default</servlet-name>
  <servlet-class >org.apache.catalina.servlets.defaultservlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
       <param-value>0</param-value>
    </init-param>
     <init-param>
      <param-name>listings</ Param-name>
      <param-value>true</param-value>
     </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>default</servlet-name
  <url-pattern>/</url-pattern>
</servlet-mapping>


The configuration in the%catalina_home%\conf\web.xml file will be shared by all Web applications running in the same Tomcat instance.

Introduction to the Java EE: mapping configuration for Servlet-mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.