我正在编写一个过滤器来执行特定任务,但我无法为我的过滤器设置特定的 url 模式。我的过滤器映射如下:
<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>
我的 url 模式 [/org/test/*/keys/* ]
没有像我预期的那样工作。
我正在从浏览器调用网址,例如:
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,过滤器模式应该匹配。我怎样才能做到这一点?
最佳答案
不,你不能在那里使用正则表达式。根据 Java Servlet Specification v2.4(srv.11.1 节),url-path 解释如下:
- 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.
不允许使用正则表达式。甚至没有复杂的通配符。
关于java - 我们可以在 web.xml URL 模式中使用正则表达式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8570805/