servlet容器的URL映射知识

来源:互联网 发布:shell 中执行 python 编辑:程序博客网 时间:2024/05/22 10:25

链接:http://blog.csdn.net/cooljia/article/details/187882

Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单。servlet和filter中的url-pattern有一些文章在里面的,总结了一些东西,以免遇到问题又要浪费时间。 
   

一,servlet容器对url的匹配过程: 

当 一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是 http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉, 剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下 的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下: 

1.     精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的 servlet了。 

2.     最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此 时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 

3.     扩展匹配,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action 

4.     如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet,则容器会将请求丢给default servlet(什么是default servlet?后面会讲)。 

     根据这个规则表,就能很清楚的知道servlet的匹配过程,所以定义servlet的时候也要考虑url-pattern的写法,以免出错。 

      对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一 个filter。Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。 
   

二,url-pattern详解 

         在web.xml文件中,以下语法用于定义映射: 

l. 以”/’开头以”/*”结尾的是用来做路径映射的。 

2. 以前缀”*.”开头的是用来做扩展映射的。 

3. “/” 是用来定义default servlet映射的。 

4. 剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action 

所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断


                        |-- Context Path --|-- Servlet Path -|--Path Info--|http://www.myserver.com     /mywebapp        /helloServlet      /hello                        |-------- Request URI  ----------------------------|

Remember the following three points:
1. Request URI = context path + servlet path + path info.
2. Context paths and servlet paths start with a / but do not end with it.
3. HttpServletRequest provides three methods getContextPath(),
    getServletPath() and getPathInfo() to retrieve the context path,
    the servlet path, and the path info, respectively, associated with a request.


Identifying the servlet path
To match a request URI with a servlet, the servlet container follows a simple algorithm.
Once it identifies the context path, if any, it evaluates the remaining part of the
request URI with the servlet mappings specified in the deployment descriptor, in the
following order. If it finds a match at any step, it does not take the next step.

The container tries to match the request URI to a servlet mapping. If it finds a
match, the complete request URI (except the context path) is the servlet path. In
this case, the path info is null.
2 It tries to recursively match the longest path by stepping down the request URI
path tree a directory at a time, using the / character as a path separator, and determining
if there is a match with a servlet. If there is a match, the matching part
of the request URI is the servlet path and the remaining part is the path info.
3 If the last node of the request URI contains an extension (.jsp, for example),
the servlet container tries to match it to a servlet that handles requests for the
specified extension. In this case, the complete request URI is the servlet path
and the path info is null.
4 If the container is still unable to find a match, it will forward the request to the
default servlet. If there is no default servlet, it will send an error message indicating
the servlet was not found.



<servlet-mapping>
    <servlet-name>RedServlet</servlet-name>
    <url-pattern>/red/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RedServlet</servlet-name>
    <url-pattern>/red/red/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RedBlueServlet</servlet-name>
    <url-pattern>/red/blue/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>BlueServlet</servlet-name>
    <url-pattern>/blue/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>GreenServlet</servlet-name>
    <url-pattern>/green</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ColorServlet</servlet-name>
    <url-pattern>*.col</url-pattern>
</servlet-mapping>


Request URI                Servlet Used            Servlet Path        Path Info
/colorapp/red                RedServlet              /red                 null
/colorapp/red/               RedServlet              /red                 /
/colorapp/red/aaa            RedServlet              /red                 /aaa

/colorapp/red/blue/aa        RedBlueServlet          /red/blue            /aa
/colorapp/red/red/aaa        RedServlet              /red/red             /aaa
/colorapp/aa.col             ColorServlet            /aa.col              null

/colorapp/hello/aa.col       ColorServlet            /hello/aa.col        null
/colorapp/red/aa.col         RedServlet              /red                 /aa.col
/colorapp/blue               NONE(Error message)                          
/colorapp/hello/blue/        NONE(Error message)                          
/colorapp/blue/mydir         NONE(Error message)
     
/colorapp/blue/dir/aa.col    ColorServlet            /blue/dir/aa.col     null  
/colorapp/green              GreenServlet            /green               null

解释一下:
这里的三个错误,都是错在blue上,注意blue的mapping url是/blue/而不是/blue或者/blue/*这是造成错误的主要原因



0 0
原创粉丝点击