0

我需要支持 /{servlet}/history,并且有许多 servlet 需要支持。我正在使用Tomcat,FWIW。

以下工作,但我想知道是否有一种方法可以将所有模式组合成一行,并避免为每个需要支持历史模式的 servlet 添加 url 模式。我尝试了几种选择,但都失败了。

<servlet>
    <servlet-name>History</servlet-name>
    <servlet-class>com.foo.HistoryServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>History</servlet-name>
    <url-pattern>/aDifferentServlet/history/*</url-pattern>
    <url-pattern>/someOtherOne/history/*</url-pattern>
    <url-pattern>/anotherExample/history/*</url-pattern>
</servlet-mapping>
...
<servlet>
    <servlet-name>aDifferentServlet</servlet-name>
    <servlet-class>com.foo.aDifferentServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>aDifferentServlet</servlet-name>
    <url-pattern>/aDifferentServlet/*</url-pattern>
</servlet-mapping>
...

谢谢。

4

2 回答 2

3

为了只有一个 URL 模式,您需要指定一个公共前缀(文件夹)模式,/history/**.history. 你不能有一个两边都有通配符匹配的 URL 模式,比如*/history/*. 最好的办法是映射历史 servlet/history/*并相应地更改 URL(例如/history/aDifferentServlet,这部分request.getPathInfo()在历史 servlet 中可用)。

如果不希望更改 URL,则只要请求 URI 与模式匹配,您就需要创建Filter或重写它们转发到历史 servlet 的 servlet */history/*

于 2012-01-27T16:55:47.320 回答
0

模式可以以星号结尾或以星号开头(表示文件扩展名映射)。

更多信息:

http://javapapers.com/servlet/what-is-servlet-mapping/#&slider1=1

The url-pattern specification:

        *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.
        *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.
于 2012-01-27T16:06:19.210 回答