12th. Mapping request to Servlet (JAVATM servlet specification 3.1)

Source: Internet
Author: User

Mapping requests to Servlets mapping requests to Servlet

The mapping techniques described in this chapter is required for WEB containers Mapping client requests to servlets.

The Web container needs to map client requests to the servlet using the mapping techniques described in this chapter.

12.1 Use of URL Paths Use URL Path

Upon receipt of a client request, the Web container determines the Web application to which to forward it. The WEB application selected must has 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.

When a client request is received, the Web container determines the forwarding to that Web App. You must select a web app that has the longest context path matching request URL to start. When mapped to a servlet, part of the URL match is the context path. The Web container next must use the path mapping procedure described below to locate the servlet to handle the request.

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

The path used to map the servlet is the request URL of the Request object minus the context path and path parameters. The following URL mapping paths are used sequentially. With the first successful match, no further matches are made:

  1. The container would try to find an exact match of the path of the the servlet. A successful match selects the servlet. The container will attempt to find the exact match of the request path to the servlet path. Select the servlet that successfully matches.
  2. The container would recursively try to match the longest path-prefix. This is do by stepping the path tree a directory at a time, using the '/' character as a path separator. The longest match determines the servlet selected. The container will recursively attempt to match the longest path prefix. Traverse the tree by one directory at a time, using the '/' character as the path separator. Select the longest matching servlet.
  3. If the last segment of the URL path contains an extension (e.g. JSP), the servlet container would try to match a servlet t Hat handles requests for the extension. An extension was defined as the part of the last segment after the last '. ' character. If the last fragment of the URL path contains an extension (for example,. jsp), the servlet container will attempt to match the sevlet used to process the request for the extension. The extension is defined as the part after the last '. ' character of the final fragment.
  4. If neither of the previous three rules result in a servlet match, the container would attempt to serve content appropriate For the resource requested. If a "default" servlet is defined for the application, it'll be used. Many containers provide an implicit the default servlet for serving content. If none of the previous three rules match a servlet, the container tries to provide the appropriate content for the requested resource. If the app defines a "default" servlet, it will be used. Many containers provide an implicit default sevlet for providing content.

The container must use case-sensitive string comparisons for matching.

The container must match with a case-sensitive string comparison.

12.2 Specification of Mappings mapping Specification

In the WEB application deployment descriptor, the following syntax are used to define mappings:

In the Web App descriptor, use the following syntax to define the mapping:

  • A string beginning with a '/' character and ending with a '/* * ' suffix is the used for path mapping.
  • A string starting with the '/' character and ending with the '/* ' suffix is used for path mapping.
  • A string beginning with a ' *. ' prefix are used as an extension mapping.
  • The string starting with the ' *. ' prefix is used as the extension mapping.
  • The empty string ("") is a special URL, pattern that exactly maps to the
    Application ' s context root, i.e, requests of the form http://host:port/<context-root>/. The the path info is '/' and the servlet path and context path is an empty string ("").
  • The empty string ("") is a special URL pattern that maps exactly to the context root of the app, for example, requests from http://host:port/<context-root>/. In this case, the path information (path info) is '/' and the servlet path and the context path are empty strings ("").
  • A string containing only the '/' character indicates the ' default ' servlet of the application. The servlet path is the request URI minus the context path and the path info is null. A string that contains only the '/' character represents the "default" Servlet for the app. In this case, the servlet path is the request URI minus the context path and the path information (path info) is null.
  • All other strings is used for exact matches only.
    If the effective Web. XML (after merging information from fragments and annotations) contains any url-patterns that is map PED to multiple servlets then the deployment must fail. Other strings are used only for exact matching. If a valid Web. XML (after merging information from fragments and annotations) contains any url-pattern matching to multiple servlets, the deployment must fail.

12.2.1 Implicit Mappings Implicit mapping

If the container has a internal JSP container, the *.jsp extension is mapped to it, allowing JSPs pages to being executed on Demand. This mapping was termed an implicit mapping. If a *.jsp mapping is defined by the WEB application, its mapping takes precedence over the implicit mapping.

A servlet container is allowed and make other implicit mappings as long as explicit mappings take precedence. For example, an implicit mapping of *.shtml could is mapped to include functionality on the server.

If the container has an internal JSP container, the *.jsp extension maps to it, allowing the JSP page to be executed as needed. This mapping is called an implicit mapping. If the reference defines a *.jsp map, this mapping takes precedence over the implicit mapping. The servlet container allows other implicit mappings as long as the display map takes precedence (higher level). For example, an implicit mapping of. sHTML can map features on the containing server.

12.2.2 Example Mapping Set Mapping Settings Instance

Consider the following set of mappings:

Consider the following mapping settings:

TABLE 12-1 Example Set of Maps

table 12-1 Mapping Settings Example:

Path Pattern Servlet

/foo/bar/* Servlet1

/baz/* Servlet2

/catalog Servlet3

*.bop Servlet4

The following behavior would result:

The following is the result of the mapping:

TABLE 12-2 Incoming Paths applied to Example Maps

table 12-2 The mappings that are applied to the example in the inbound path

Incoming Path Servlet handling Request Process the requested Servlet

/foo/bar/index.html Servlet1

/foo/bar/index.bop Servlet1

/baz Servlet2

/baz/index.html Servlet2

/catalog Servlet3

/catalog/index.html the "default" servlet ("Defaults " servlet)

/catalog/racecar.bop Servlet4

/index.bop Servlet4

Note in the case of/catalog/index.html And/catalog/racecar.bop, the servlets mapped to "/catalog" are not used Becaus E The match is not exact.

Note that/catalog/index.html and/catalog/racecar.bop, the servlet mapped to "/catalog" are not used because the match is imprecise.

12th. Mapping request to Servlet (JAVATM servlet specification 3.1)

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.