web.xml:

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

  1. 精確路徑:以 / 開始,後面是具體路徑,好比 /index.do
  2. 最長路徑:以 / 開始,以 /* 結束,好比 /index/*, /*
  3. 擴展名:以 *. 開始,以 擴展名 結束,好比 *.do
  4. 默認:單獨的 /

映射規則url

  1. 精確路徑。code

    <url-pattern>/index</url-pattern>
     匹配:
     http://localhost:8080/project/index
     http://localhost:8080/project/index?name=admin
  2. 最長路徑。xml

    <url-pattern>/index/a/*</url-pattern>
     匹配:
     http://localhost:8080/project/index/a/action 
     該URL,若是沒有第一個 url-pattern ,則可匹配:
     <url-pattern>/index/*</url-pattern>
  3. 擴展名。get

    <url-pattern>*.do</url-pattern>
     匹配:
     http://localhost:8080/project/index/a.do

注意事項servlet

  • 精確路徑、最長路徑、擴展名、默認,依次匹配,若是所有失敗,返回 404.
  • <servlet> 找到第一個,就中止以後的匹配; <filter> 會根據 <filter-mapping> 定義順序一直向下走。
相關文章
相關標籤/搜索