web.xml 中的 <url-pattern>
是 <servlet-mapping>
或 <filter-mapping>
下的子標籤。web
url :http://localhost:8080/project/index 的組成:
http://localhost:8080 服務器地址。以後的爲 RequestURI。
/project ServletConext的上下文地址,ServletContext Path。
/index Servlet的地址,Servlet Path,這部分是須要與<url-pattern>匹配的內容。服務器
寫法,只有如下四種寫法:app
/
開始,後面是具體路徑,好比 /index.do
。/
開始,以 /*
結束,好比 /index/*, /*
。*.
開始,以 擴展名 結束,好比 *.do
。/
。映射規則:url
精確路徑。code
<url-pattern>/index</url-pattern> 匹配: http://localhost:8080/project/index http://localhost:8080/project/index?name=admin
最長路徑。xml
<url-pattern>/index/a/*</url-pattern> 匹配: http://localhost:8080/project/index/a/action 該URL,若是沒有第一個 url-pattern ,則可匹配: <url-pattern>/index/*</url-pattern>
擴展名。get
<url-pattern>*.do</url-pattern> 匹配: http://localhost:8080/project/index/a.do
注意事項:servlet
<servlet>
找到第一個,就中止以後的匹配; <filter>
會根據 <filter-mapping>
定義順序一直向下走。