Blueherb In solitude, where we are least alone

web.xml:<url-pattern>

web.xml 中的 <url-pattern><servlet-mapping><filter-mapping> 下的子标签。

url :http://localhost:8080/project/index 的组成:
http://localhost:8080 服务器地址。之后的为 RequestURI。
/project ServletConext的上下文地址,ServletContext Path。
/index Servlet的地址,Servlet Path,这部分是需要与<url-pattern>匹配的内容。

写法,只有以下四种写法:

  1. 精确路径:以 / 开始,后面是具体路径,比如 /index.do
  2. 最长路径:以 / 开始,以 /* 结束,比如 /index/*, /*
  3. 扩展名:以 *. 开始,以 扩展名 结束,比如 *.do
  4. 默认:单独的 /

映射规则

  1. 精确路径。
     <url-pattern>/index</url-pattern>
     匹配:
     http://localhost:8080/project/index
     http://localhost:8080/project/index?name=admin
    
  2. 最长路径。
     <url-pattern>/index/a/*</url-pattern>
     匹配:
     http://localhost:8080/project/index/a/action 
     该URL,如果没有第一个 url-pattern ,则可匹配:
     <url-pattern>/index/*</url-pattern>
    
  3. 扩展名。
     <url-pattern>*.do</url-pattern>
     匹配:
     http://localhost:8080/project/index/a.do 
    

注意事项

  • 精确路径、最长路径、扩展名、默认,依次匹配,如果全部失败,返回 404.
  • <servlet> 找到第一个,就停止之后的匹配; <filter> 会根据 <filter-mapping> 定义顺序一直向下走。
posted @ 2019-03-15 16:17  Blueherb  阅读(708)  评论(0编辑  收藏  举报