<servlet-mapping>
<servlet-name>defaultservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>jspservlet-name>
<url-pattern>*.jspurl-pattern>
<url-pattern>*.jspxurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>helloServletservlet-name>
<servlet-class>xin.yangshuai.servlet.HelloServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet>
<servlet-name>hiServletservlet-name>
<servlet-class>xin.yangshuai.servlet.HiServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hiurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hi/*url-pattern>
servlet-mapping>
key | value |
---|---|
/hi | hiServlet |
/hello | helloServlet |
/* | helloServlet |
/hi/* | hiServlet |
.jsp | jsp |
.jspx | jsp |
/ | helloServlet |
key | value | 匹配规则 | 对象名 | 对象类型 |
---|---|---|---|---|
/hello | helloServlet | 精确匹配 | exactWrappers | Mapper.MappedWrapper[] |
/hi | hiServlet | 精确匹配 | exactWrappers | Mapper.MappedWrapper[] |
(空串) | helloServlet | 前缀匹配 | wildcardWrappers | Mapper.MappedWrapper[] |
/hi | hiServlet | 前缀匹配 | wildcardWrappers | Mapper.MappedWrapper[] |
jsp | jsp | 扩展名匹配 | extensionWrappers | Mapper.MappedWrapper[] |
jspx | jsp | 扩展名匹配 | extensionWrappers | Mapper.MappedWrapper[] |
(空串) | helloServlet | 默认匹配 | defaultWrapper | Mapper.MappedWrapper |
注意:这里的对象名和对象类型都是java类中使用的,可以去看源码
匹配规则 | 对象名 | 对象类型 | 映射特征(url-pattern) |
---|---|---|---|
精确匹配 | exactWrappers | 数组 | 具体路径,例如:/hello |
前缀匹配 | wildcardWrappers | 数组 | 以/*结尾,例如:/*,/hi/* |
扩展名匹配 | extensionWrappers | 数组 | 以*.开头,例如:*.jsp,*.html |
默认匹配 | defaultWrapper | 一个对象 | /,其它匹配都无法匹配时,使用默认匹配 |
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
仍然使用 前文提到的项目 ,servlet和项目结构都不变。项目的web.xml发生变化,覆盖tomcat自带的web.xml的welcome-file-list,现在web.xml内容如下:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>helloServletservlet-name>
<servlet-class>xin.yangshuai.servlet.HelloServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet>
<servlet-name>hiServletservlet-name>
<servlet-class>xin.yangshuai.servlet.HiServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/index.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/index.jspurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
运行结果:hiServlet配置了/*映射,则由hiServlet来处理默认访问
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/index.jspurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/index.htmlurl-pattern>
servlet-mapping>
运行结果:没有servlet配置/*,只有一个welcome-file,值为index.html,则默认访问相当于:http://localhost:8080/first_page/index.html,hiServlet配置的精确匹配,所有由hiServlet来处理默认访问。
<welcome-file-list>
<welcome-file>hh/index.htmlwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/hh/*url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hh/index.htmlurl-pattern>
servlet-mapping>
运行结果::没有servlet配置/*,只有一个welcome-file,则默认访问相当于:http://localhost:8080/first_page/hh/index.html,hiServlet配置的精确匹配,所以由hiServlet来处理默认访问。
注意:没有精确匹配时,也会寻找可以进行前缀匹配、扩展名匹配的servlet进行匹配。
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hiurl-pattern>
servlet-mapping>
运行结果:没有servlet配置/*,存在多个welcome-file时,按照顺序依次尝试,首先尝试拼接index.html,但是index.html并非物理真实存在,并且不能精确匹配或者前缀匹配,所以index.html被舍弃,同样道理,index.htm也会被舍弃,然后尝试index.jsp,index.jsp物理真实存在,所以拼接index.jsp,默认访问相当于:http://localhost:8080/first_page/index.jsp,此路径可以由tomcat自带的名为jsp的servlet进行扩展名(*.jsp)匹配,这也就是我们新建项目默认访问的页面。
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>*.jspurl-pattern>
servlet-mapping>
运行结果:与上面相同的分析过程相同,默认访问相当于:http://localhost:8080/first_page/index.jsp,由于hiServlet覆盖了*.jsp映射规则,所以由hiServlet处理默认访问。
<welcome-file-list>
<welcome-file>hh/index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.jspurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hh/*url-pattern>
servlet-mapping>
运行结果:没有servlet配置/*,存在多个welcome-file时,按照顺序依次尝试,首先尝试拼接hh/index.html,可以看出,hiServlet配置了/hh/*的映射规则,符合前缀匹配,所以拼接hh/index.html,默认访问相当于:http://localhost:8080/first_page/hh/index.html,所以由hiServlet处理默认访问。
注意:除了依据前缀匹配,也可以依据精确匹配。
7、尝试拼接:不能依据扩展名匹配
web.xml
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/hhurl-pattern>
servlet-mapping>
运行结果:没有servlet配置/*,存在多个welcome-file时,按照顺序依次尝试,此时默认访问相当于:http://localhost:8080/first_page/index.jsp,依据物理真实存在文件拼接。helloServlet虽然配置了*.html后缀匹配规则,但是并没有拼接index.html。
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>hiServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
运行结果:没有servlet配置/*,存在多个welcome-file时,按照顺序依次尝试,此时依据精确匹配,前缀匹配和物理真实存在文件匹配都无法匹配到,所以拼接第一个,默认访问相当于:http://localhost:8080/first_page/index.html,此时只有helloServlet配置了*.html扩展名匹配,所以由helloServlet来处理默认访问。
如有错误,欢迎指正!!!
参考:https://www.cnblogs.com/fangjian0423/p/servletContainer-tomcat-urlPattern.html