我可以将这些 url 模式组合到 servlet 映射中吗?

发布于 2024-12-29 12:00:52 字数 1019 浏览 2 评论 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>
...

谢谢。

I have a requirement to support /{servlet}/history, and have many servlets that need to support this. I'm using Tomcat, FWIW.

The following works, but I'm wondering if there's a way to combine all patterns into one line and avoid adding a url-pattern for every servlet that needs to support the history pattern. I've tried several options and failed.

<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>
...

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2025-01-05 12:00:52

为了只有一种 URL 模式,您需要指定一个通用前缀(文件夹)模式,如 /history/* 或后缀(扩展名)模式,如 *.history.您不能使用两侧都带有通配符匹配的 URL 模式,例如 */history/*。最好的办法是将历史 servlet 映射到 /history/* 上,并相应地更改 URL,例如 /history/aDifferentServlet (这部分可以通过 >request.getPathInfo() 在历史 servlet 中)。

如果不希望更改 URL,则需要创建一个 Filter 或重写 servlet,只要请求 URI 与 */history/* 匹配,它们就会转发到历史 servlet > 图案。

In order to have only one URL pattern, you'd need to specify a common prefix (folder) pattern like /history/* or a suffix (extension) pattern like *.history. You cannot have an URL pattern with wildcard matches on both sides like */history/*. Your best bet is to have the history servlet mapped on /history/* and change the URLs accordingly to for example /history/aDifferentServlet (this part is then available by request.getPathInfo() in the history servlet).

If changing the URLs is undesireable, you'd need to create a Filter or rewrite the servlets that they forward to the history servlet whenever the request URI matches the */history/* pattern.

夜深人未静 2025-01-05 12:00:52

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

更多信息请访问:

http://javapapers.com/servlet/什么是 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.

pattern can either end in an asterisk or start with one (to denote a file extension mapping).

more info at:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文