我们可以在web.xml URL模式中使用正则表达式吗? [英] Can we use regular expressions in web.xml URL patterns?
问题描述
我正在编写一个过滤器来执行特定任务,但我无法为我的过滤器设置特定的url模式。我的过滤器映射如下:
I am writing a filter to do a specific task but I am unable to set a specific url pattern to my filter. My filter mapping is as follows:
<web.xml>
<filter>
<filter-name>myFilter</filter-name>
<filter-class>test.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/org/test/*/keys/*</url-pattern>
</filter-mapping>
</web-app>
我的网址格式 [/ org / test / * / keys / * ]
无法正常工作。
My url-pattern [ /org/test/ * /keys/ * ]
is not working as I had expected.
我正在通过浏览器调用网址:
I am calling urls from the browser like:
http://localhost:8080/myapp/org/test/SuperAdminReport/keys/superAdminReport.jsp
http://localhost:8080/myapp/org/test/Adminreport/keys/adminReport.jsp
http://localhost:8080/myapp/org/test/OtherReport/keys/otherReport.jsp
因此,对于上面的URL,过滤器模式应匹配。我怎样才能实现这个目标?
So for the URLs above the filter pattern should match. How can I achieve this?
推荐答案
不,你不能在那里使用正则表达式。根据Java Servlet Specification v2.4(section srv.11.1),url-path被解释如下:
No, you can't use a regex there. According to the Java Servlet Specification v2.4 (section srv.11.1), the url-path is interpreted as follows:
- 以'/'字符开头并以'/ *'后缀结尾的字符串用于路径映射。
- 以'*。'前缀开头的字符串是用作扩展映射。
- 仅包含'/'字符的字符串表示应用程序的默认servlet。在这种情况下,servlet路径是
请求URI减去条件路径,路径信息为空。
- 所有其他字符串仅用于完全匹配。
- 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 con- text path and the path info is null.
- All other strings are used for exact matches only.
不允许使用正则表达式。甚至没有复杂的外卡。
No regexes are allowed. Not even complicated wild-cards.
这篇关于我们可以在web.xml URL模式中使用正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!