url-pattern定义及匹配

web.xml之url-pattern定义规则及匹配过程

译自《Servlet Specification Version 2.4》,转载请注明出处!

把请求映射到Servlet
    在这个章节中描述的映射技术都需要web容器把客户端的请求映射到Servlet。
SRV.11.1 使用URL路径
    当收到一个客户端请求时,web容器决定将请求转交给那个web应用处理。被选择的web应用必须是从前向后匹配的请求URL的最长的上下文路径,匹配的URL部分就是映射到Servlet时的上下文路径。
    Web容器下一步必须使用下面描述的路径映射规则找到处理请求的Servlet。
    用于映射到Servlet的路径是请求的URL去掉上下文路径和路径参数(如http://localhost/myapp/a /a.jsp?msg=hello,上下文路径为/myapp,路径参数为?msg=hello)。按序使用以下URL路径映射规则,第一次匹配成功的规 则将被使用,并且将不会尝试继续匹配:
    1. 容器将试图为请求的路径查找一个精确匹配的Servlet路径。匹配成功则选择该Servlet。
    2. 容器将递归的匹配最长路径前缀,这是通过使用字符'/'做路径分隔符从后往前分隔路径树,一次一个目录,最长的匹配决定了该Servlet被选择。
    3. 如果URL路径最后的部分包含一个扩展名(如.jsp),Servlet容器将尝试匹配一个处理该后缀请求的Servlet,扩展名定义为最后一个'/'后面的部分中字符'.'后的部分。
    4. 如果前面的三个规则都不能匹配到一个Servlet,容器将尝试为请求的资源使用内容服务,如果应用定义了默认的Servlet,则它将被使用。
    容器的匹配过程必须使用大小写敏感的字符串比较。
SRV.11.2 映射详述
在Web应用部署描述符中,下面的语法被用于定义映射:
    • 以'/'开头且以‘/*’结束的字符串用于路径映射。
    • 以'*.'开头的字串串用于扩展名映射。
    • 仅包含'/'的字符串表明对应的Servlet为应用默认的Servlet。在这种情况下Servlet路径是请求的URI去掉上下文路径并且路径信息为null。
    • 所有其它的字符串都被只能用于精确匹配。
SRV.11.2.1 隐式映射
如果容器包含一个内部的JSP容器,则*.jsp扩展名将映射到该容器。允许JSP页面一经请求就执行。这种映射称为隐式映射。如果*.jsp映射被定义成web应用,其优先级高于隐式映射。
一个Servlet容器允许做更多的隐式映射当明确的映射具有高优先级时。例如,*.shtml的隐式映射可以被用于映射成在服务器上包含具体的功能。
SRV.11.2.2 映射例子
思考下面的映射:

Table SRV.11-1 Example Set of Maps
Path Pattern        Servlet
/foo/bar/*             servlet1
/baz/*                   servlet2
/catalog               servlet3
*.bop                    servlet4
紧接着是映射到结果:

Table SRV.11-2 Incoming Paths Applied to Example Maps
Incoming Path Servlet Handling        Request
/foo/bar/index.html                            servlet1
/foo/bar/index.bop                              servlet1
/baz                                                      servlet2
/baz/index.html                                    servlet2
/catalog                                                servlet3
/catalog/index.html                            “default” servlet
/catalog/racecar.bop                          servlet4
/index.bop                                            servlet4
注意例子中的/catalog/index.html和/catalog/racecar.bop并没有映射到/catalog,因为/catalog是一个精确映射。

------------------------------------------------------------------------------------------------------------------------------------

Mapping Requests to Servlets
    The mapping techniques described in this chapter are required for Web containers
mapping client requests to servlets.
SRV.11.1 Use of URL Paths
     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.
SRV.11.2 Specification of Mappings
In theWeb application deployment descriptor, the following syntax is used to define
mappings:
    • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used
      for path mapping.
    • A string beginning with a ‘*.’ prefix is used as an extension mapping.
    • 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.
    • All other strings are used for exact matches only.
SRV.11.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.
SRV.11.2.2 Example Mapping Set
Consider the following set of mappings:

Table SRV.11-1 Example Set of Maps
Path Pattern        Servlet
/foo/bar/*             servlet1
/baz/*                   servlet2
/catalog               servlet3
*.bop                    servlet4
The following behavior would result:

Table SRV.11-2 Incoming Paths Applied to Example Maps
Incoming Path Servlet Handling        Request
/foo/bar/index.html                            servlet1
/foo/bar/index.bop                            servlet1
/baz                                                  servlet2
/baz/index.html                                 servlet2
/catalog                                            servlet3
/catalog/index.html                            “default” 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.

你可能感兴趣的:(html,jsp,应用服务器,Web,servlet)