JavaWeb开发中,servlet的url-pattern的映射规则
来源:互联网 发布:赛门铁克备份软件 编辑:程序博客网 时间:2024/06/08 17:02
原文出处:http://www.cnblogs.com/mailingfeng/archive/2012/04/05/2432687.html
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”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。
另外,关于url-pattern映射之后, request的servletContextPath , ServletPath , PathInfo 情况,可参照下面链接的文章
http://blog.csdn.net/cooljia/article/details/187882
=========================================================================================================
附上servlet3.1规范中的Mapping requests to servlet(12.1,12.2)
12.Mapping Requests to Servlets
The mapping techniques described in this chapter are required for Web containersmapping client requests to servlets.
12.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 applicatio n 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.
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 matc h 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-pre fix. 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. Many containers provide an
implicit default servlet for serving content.
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:
■ 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.
■ The empty string ("") is a special UR L 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 (““).
■ 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.
If the effective web.xml (after merging information from fragments and
annotations) contains any url-patterns that are mapped to multiple servlets then thedeployment must fail.
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 ot her implicit mappings as long as explicit
mappings take precedence. For example, an implicit mapping of *.shtml could bemapped to include functionality on the server.
- JavaWeb开发中,servlet的url-pattern的映射规则
- web.xml中的Servlet的url-pattern映射规则
- javaweb中Filter和Servlet的url-pattern配置解析
- java--web.xml中url-pattern的映射规则
- [url-pattern] servlet的url-pattern匹配规则详细描述
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- Servlet的url-pattern匹配规则
- servlet的url-pattern匹配规则
- servlet的url-pattern匹配规则
- Servlet url-pattern的匹配规则
- Servlet HTTP 状态码
- FreeRTOS(V8.0.1)系统之vTaskSuspendAll()和xTaskResumeAll()
- TVS参数详解及选型应用
- wpf学习-根据绑定数据有条件地改变元素的属性
- 百度地图ios 启动报manager start failed的问题
- JavaWeb开发中,servlet的url-pattern的映射规则
- 关于hash质数
- java Hibernate一级缓存和二级缓存概念案例详解
- 又见皇后!!!
- 通过中间人攻击的方法拦截传输在https上的加密信息
- 设计模式
- Android4.2中应用接收开机广播
- MySQL实现单机多实例安装并配置主从复制
- V4L2驱动程序架构