url-pattern配置技巧

时间:2024-02-15 21:36:16

配置Filter-mapping时,配置如下:

 <filter-mapping>
  <filter-name>aFilter</filter-name>
  <url-pattern>/a/*</url-pattern>
 </filter-mapping>
希望能过滤网站a地址下的所有请求。在实际的运行中,它确实能过滤a地址下的,不会过滤b地址下的。但是对于不在子目录下的请求(假如我的站点叫demo),如:http://localhost:8080/demo/aDemo.do,这个请求地址竟然能和 <url-pattern>/a/*</url-pattern>匹配。这显然是错误的。我们仅希望能过滤http://localhost:8080/demo/a/*.do

经查询资料,修改映射为:

 <filter-mapping>
  <filter-name>aFilter</filter-name>
  <url-pattern>/a/*.*</url-pattern>
 </filter-mapping>

Ok!成功!

 

配置servlet的<url-pattern>时,容器会首先查找完全匹配,如果找不到,再查找目录匹配,如果也找不到,就查找扩展名匹配。 如果一个请求匹配多个“目录匹配”,容器会选择最长的匹配。

① 完全匹配

  1. <!-- 必须以一个“/”开头,可以有扩展名,但不是必需的。 -->  
  2. <url-pattern>/test/list.do</url-pattern>  

② 目录匹配

  1. <!-- 必须以一个“/”开头,目录可以是虚拟或实际的,总是以一个“/*”结束 -->  
  2. <url-pattern>/test/*</url-pattern>  

③ 扩展名匹配

  1. <!-- 必须以“*”加“.”加扩展名的形式 -->  
  2. <url-pattern>*.do</url-pattern>  

如果有人还不理解,可看看此文章

 <转:http://blog.sina.com.cn/s/blog_4cab775e01000a1w.html>

 

 

 #########################################################################################
##########################################################################################

 

 

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 URL pattern that exactly maps to the application\'s context root, i.e., requests of the form http://host:port/<contextroot>/. 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.

12.2 映射的规定

在 Web 应用部署描述符中,用于映射语法的规定如下:

■ 以“/”字符开头,并以“/*”结尾的字符串用于路径映射。
■ 以“*.”开头的字符串被用于扩展名映射。
■ 空字符串("")是用于指定精确映射应用程序 context root 的 URL 模式,比如从 http://host:port/<contextroot>/ 来的请求。在这种情况下路径信息是“/”,servlet 路径和 context 路径是一个空的字符串("")。
■ 字符串中仅有“/”字符时,表示应用程序“默认”的 servlet。在这种情况下 servlet 路径是请求 URI 去掉 context 路径,且路径信息为 null。
■ 其他的字符串仅用于精确匹配。

 

 

#########################################################################################
##########################################################################################

 

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

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

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

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

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