第十二章 映射请求到Servlet(JavaTM Servlet 规范3.1 )

Mapping Requests to Servlets 映射请求到 Servlet

The mapping techniques described in this chapter are required for Web containers mapping client requests to servlets.

Web 容器需要使用本章描述的映射技术将客户端请求映射到 servlet。

12.1 Use of URL Paths 使用 URL 路径

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 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. 

当接收到一个客户端请求,Web容器确定转发到那个Web应用。必须选择拥有最长上下文路径匹配请求 URL 开始的Web应用。当映射到 servlet 时,URL匹配的一部分是上下文路径。Web容器接下来必须使用下面描述的路径映射过程来定位 servlet 来处理请求。

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: 

用来映射 servlet 的路径是请求对象的请求URL 减去上下文路径和路径参数。下面的URL映射路径按顺序使用。使用第一个成功的匹配,不会再做更一步的匹配:

  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. 容器将尝试找出请求路径到 servlet 路径的精确匹配。选择成功匹配的 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. 容器将递归尝试匹配最长的路径前缀。通过一次一个目录遍历路径树,使用 ‘/‘ 字符作为路径分割符来完成。选择最长匹配的 servlet。
  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. 如果URL路径的最后片段包含一个扩展名(例如 .jsp),servlet 容器将尝试匹配用来为扩展名处理请求的 sevlet。扩展名被定义为最后片段最后 ‘.’ 字符之后的部分。 
  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. Many containers provide an implicit default servlet for serving content.如果前面的三个规则都没有匹配一个 servlet ,容器将尝试为请求的资源提供合适的内容。如果应用定义了一个“默认”servlet,它将被使用。很多容器为提供内容提供了一个隐性默认sevlet 。

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

容器必须使用大小写敏感的字符串比较来匹配。

12.2 Specification of Mappings 映射规范

In the Web application deployment descriptor, the following syntax is used to define mappings: 

在 web 应用描述符中,使用下面的语法来定义映射:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping. 
  • 以 ‘/‘ 字符开始(译者注:这里的 and 不是“”,否则 <url-pattern>/servletx</url-pattern> 这样的设置岂不是有问题?,如:/catalog,/catalog/*,/* 都应该是合法的 url-pattern)以 ‘/*’ 后缀结尾的字符串用来路径映射。
  • A string beginning with a ‘*.’ prefix is used as an 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>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““). 
  • 空字符串(””)是一个特殊的 URL pattern ,精确映射到应用的上下文根,例如,来自  http://host:port/<context- root>/ 的请求。在这种情况下,路径信息(path info)是 ‘/‘ 并且 servlet 路径和上下文路径是空字符串(“”)。
  • 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. 一个只包含 ‘/‘ 字符的字符串表示应用的 “默认” servlet 。在这种情况下,servlet 路径是请求 URI 减去上下文路径并且路径信息(path info)是 null。
  • All other strings are used for exact matches only.
    If the effective web.xml (after merging information from fragments and annotations) contains any url-patterns that are mapped to multiple servlets then the deployment must fail. 其他字符串仅用来进行精确匹配。如果有效的 web.xml (合并来自片段和注解的信息之后)包含任何匹配到多个 servlet 的 url-pattern,那么部署必须失败。

12.2.1 Implicit Mappings 隐含映射

If the container has an internal JSP container, the *.jsp extension is mapped to it, allowing JSP pages to be executed on demand. This mapping is 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 to make other implicit mappings as long as explicit mappings take precedence. For example, an implicit mapping of *.shtml could be mapped to include functionality on the server. 

如果容器有一个内部的 JSP 容器,*.jsp 扩展名将映射到它,允许根据需要执行 JSP 页面。此映射被称为隐含映射。如果引用定义了 *.jsp 映射,此映射优先于隐含映射。servlet 容器允许其他的隐式映射只要显示映射优先(级更高)。例如,.shtml 的隐式映射可以映射包含服务器上的功能。

12.2.2 Example Mapping Set 映射设置实例

Consider the following set of mappings: 

思考下面的映射设置:

TABLE 12-1 Example Set of Maps 

表 12-1 映射设置示例:

Path Pattern   Servlet 

/foo/bar/*     servlet1

/baz/*        servlet2

/catalog      servlet3

*.bop       servlet4

The following behavior would result: 

以下是映射的结果:

TABLE 12-2 Incoming Paths Applied to Example Maps 

表 12-2 入站路径应用到示例中的映射

Incoming Path        Servlet Handling Request  处理请求的 Servlet

/foo/bar/index.html      servlet1

/foo/bar/index.bop     servlet1

/baz             servlet2

/baz/index.html       servlet2

/catalog           servlet3

/catalog/index.html      “default” servlet (“默认” servlet)

/catalog/racecar.bop    servlet4   

/index.bop          servlet4

Note that in the case of /catalog/index.html and /catalog/racecar.bop, the servlet mapped to “/catalog” is not used because the match is not exact. 

注意 /catalog/index.html 和 /catalog/racecar.bop,映射到 “/catalog” 的 servlet 没有被使用,因为匹配不精确。

原文地址:https://www.cnblogs.com/whilliy/p/5700718.html