我有这个映射
<servlet-mapping> <servlet-name>service</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
但我也想要
/服务/主
映射到主servlet.
<servlet-mapping> <servlet-name>master</servlet-name> <url-pattern>/service/master</url-pattern> </servlet-mapping>
我相信这里存在冲突,因为调用/ service / *会立即触发服务servlet.有没有办法让我在servlet映射中使用某种排除,或者可以使用regexp来做我想做的事情?
解决方法
Servlet映射始终使用最具体的匹配,因此路径< context> / service / master将始终映射到master.
这是Servlet 3.0 spec的第一个映射规则:
- The container will try to find an exact match of the path of the request
to the path of the servlet. A
successful match selects the servlet.- The container will recursively try to match the longest path-prefix. This
is done by stepping down the path tree
a directory at a time,using the ’/’
character as a path separator. The
longest match determines the servlet
selected.- If the last segment in the URL path contains an extension (e.g.
.jsp),the servlet container will try
to match a servlet that handles
requests for the extension. An
extension is defined as the part of
the last segment after the last ’.’
character.- If neither of the prevIoUs three rules result in a servlet match,the container will attempt to serve content appropriate for the resource requested. If a “default” servlet is defined for the application,it will be used. Many containers provide an implicit default servlet for serving content.