1.精确匹配
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/users.html</url-pattern> <url-pattern>/index.html</url-pattern> <url-pattern>/user/addUser.action</url-pattern> </servlet-mapping>
当在浏览器中输入如下几种url时,都会被匹配到该servlet
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/index.html
http://localhost:8080/appDemo/user/addUser.action
2.扩展名匹配
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern> </servlet-mapping>
则任何扩展名为jsp或action的url请求都会匹配,比如下面的url都会被匹配
http://localhost:8080/appDemo/user/users.jsp
http://localhost:8080/appDemo/toHome.action
3.路径匹配
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>
路径以/user/开始,后面的路径可以任意。比如下面的url都会被匹配。
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/user/addUser.action
http://localhost:8080/appDemo/user/updateUser.actionl
4.缺省匹配
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
匹配顺序:
- 精确匹配,servlet-mapping1:<url-pattern>/user/users.html</url-pattern>,servlet-mapping2:<url-pattern>/*</url-pattern>。当一个请求http://localhost:8080/appDemo/user/users.html来的时候,servlet-mapping1匹配到,不再用servlet-mapping2匹配
- 路径匹配,先最长路径匹配,再最短路径匹配servlet-mapping1:<url-pattern>/user/*</url-pattern>,servlet-mapping2:<url-pattern>/*</url-pattern>。当一个请求http://localhost:8080/appDemo/user/users.html来的时候,servlet-mapping1匹配到,不再用servlet-mapping2匹配
- 扩展名匹配,servlet-mapping1:<url-pattern>/user/*</url-pattern>,servlet-mapping2:<url-pattern>*.action</url-pattern>。当一个请求http://localhost:8080/appDemo/user/addUser.action来的时候,servlet-mapping1匹配到,不再用servlet-mapping2匹配
- 缺省匹配,以上都找不到servlet,就用默认的servlet,配置为<url-pattern>/</url-pattern>