先引用部分 Servlet3.0 Specification
原文
match rule
The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters. The URL path mapping rules below are used in order.The first successful match is used with no further matches attempted:
The container must use case-sensitive string comparisons for matching.
urlpattern syntax
In the Web application deployment descriptor, the following syntax is used to define mappings:
A string beginning with
a ‘/’
character and ending with
a ‘/*’
suffix is used for path mapping.
A string beginning with
a ‘*.’
prefix is used as an extension
mapping.
The empty
string ("")
is a special URL pattern that exactly maps
to the application‘s context root, i.e., requests of the
form http://host:port/<contextroot>/
. In this case the
path info is ’/’
and the servlet path and context path
is empty string (““)
.
A string containing only
the ’/’
character indicates the "default" servlet of
the application. In this case the servlet path is the request URI minus the
context path and the path info is null.
All other strings are used for exact matches only.
什么是DefaultServlet呢?
事实上就是 org.apache.catalina.servlets.DefaultServlet 可以参考tomcat/conf中的web.xml
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
SpringMVC中 /* 和 /
在tomcat/conf中的web.xml中还可以发现
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
SpringMVC 的 DispatcherServlet
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
</web-app>
<url-pattern> / </url-pattern>:覆盖掉DefaultServlet,但隐式的 JspServlet 仍然起作用
<url-pattern> /* </url-pattern>:会导致jsp无法解析
SpringMVC的静态资源
mvc:resources 把mapping的URI注册到SimpleUrlHandlerMapping的urlMap中
mvc:default-servlet-handler 会把"/**" url,注册到SimpleUrlHandlerMapping的urlMap中,把对静态资源的访问由HandlerMapping转到org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler处理并返回.
DefaultServletHttpRequestHandler使用就是各个Servlet容器自己的默认Servlet.
原文:http://www.cnblogs.com/cozybz/p/3530965.html