url-pattern配置技巧


配置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?后面會講)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM