I have this one mapping
我有这个映射
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
but i also want /service/master to map to master servlet.
但我也希望/ service / master映射到主servlet。
<servlet-mapping>
<servlet-name>master</servlet-name>
<url-pattern>/service/master</url-pattern>
</servlet-mapping>
I believe there is a conflict here since calling /service/* will trigger service servlet right away. Is there a way for me to use some kind of exclusion in servlet-mapping or may be regexp to do what I want to do?
我相信这里存在冲突,因为调用/ service / *会立即触发服务servlet。有没有办法让我在servlet映射中使用某种排除,或者可以使用regexp来做我想做的事情?
2 个解决方案
#1
6
Servlet mappings always use the most specific match, so the path <context>/service/master
will always map to master
.
Servlet映射始终使用最具体的匹配,因此路径
This is the 1st rule of mappings from the Servlet 3.0 spec:
这是Servlet 3.0规范中映射的第一条规则:
- 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.
容器将尝试查找请求路径与servlet路径的完全匹配。成功匹配选择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.
容器将递归地尝试匹配最长的路径前缀。这是通过使用'/'字符作为路径分隔符一次单击目录的路径树来完成的。最长匹配确定所选的servlet。
- 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.
如果URL路径中的最后一个段包含扩展名(例如.jsp),则servlet容器将尝试匹配处理扩展请求的servlet。扩展名被定义为最后一个“。”字符后的最后一个段的一部分。
- 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.
如果前三个规则都不会导致servlet匹配,则容器将尝试提供适合所请求资源的内容。如果为应用程序定义了“默认”servlet,则将使用它。许多容器提供用于提供内容的隐式默认servlet。
#2
1
You can try using Google Guice. com.google.inject.servlet.ServletModule.serveRegex(String regex, String... regexes) will let you use regex in mapping.
您可以尝试使用Google Guice。 com.google.inject.servlet.ServletModule.serveRegex(String regex,String ... regexes)将允许您在映射中使用正则表达式。
see here http://code.google.com/p/google-guice/wiki/ServletModule
请参阅http://code.google.com/p/google-guice/wiki/ServletModule
#1
6
Servlet mappings always use the most specific match, so the path <context>/service/master
will always map to master
.
Servlet映射始终使用最具体的匹配,因此路径
This is the 1st rule of mappings from the Servlet 3.0 spec:
这是Servlet 3.0规范中映射的第一条规则:
- 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.
容器将尝试查找请求路径与servlet路径的完全匹配。成功匹配选择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.
容器将递归地尝试匹配最长的路径前缀。这是通过使用'/'字符作为路径分隔符一次单击目录的路径树来完成的。最长匹配确定所选的servlet。
- 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.
如果URL路径中的最后一个段包含扩展名(例如.jsp),则servlet容器将尝试匹配处理扩展请求的servlet。扩展名被定义为最后一个“。”字符后的最后一个段的一部分。
- 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.
如果前三个规则都不会导致servlet匹配,则容器将尝试提供适合所请求资源的内容。如果为应用程序定义了“默认”servlet,则将使用它。许多容器提供用于提供内容的隐式默认servlet。
#2
1
You can try using Google Guice. com.google.inject.servlet.ServletModule.serveRegex(String regex, String... regexes) will let you use regex in mapping.
您可以尝试使用Google Guice。 com.google.inject.servlet.ServletModule.serveRegex(String regex,String ... regexes)将允许您在映射中使用正则表达式。
see here http://code.google.com/p/google-guice/wiki/ServletModule
请参阅http://code.google.com/p/google-guice/wiki/ServletModule