java - 我们可以在 web.xml URL 模式中使用正则表达式吗?

标签 java servlet-filters web.xml

我正在编写一个过滤器来执行特定任务,但我无法为我的过滤器设置特定的 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/

相关文章:

java - 我想自动同时滑动 3 View 寻呼机

java - 已为此响应调用 Tomcat 8.5 response.getWriter()

java - 阻止用户向页面发出 GET 请求

java - 为什么 java.net.URLEncoder 对同一个字符串给出不同的结果?

java - 在独立应用程序中, session 对象存储在哪里?

java - 云端点 : Access Paramters in Servlet Filter

jsf - FacesContext#getCurrentInstance() 在 Filter#doFilter() 中返回 null

java - 将 Spring 与 WebSphere 8.5.5 和 Apache Wink 结合使用 - 服务器启动时出现 web.xml 错误

spring - HttpSessionListener 实现内部的依赖注入(inject)

java - 我的自定义控件应该依赖于 Controller 类还是接口(interface)?