我可以将这些 url 模式组合到 servlet 映射中吗?
我需要支持 /{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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了只有一种 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 byrequest.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.模式可以以星号结尾或以星号开头(表示文件扩展名映射)。
更多信息请访问:
http://javapapers.com/servlet/什么是 servlet-mapping/#&slider1=1
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