查找多servlet映射场景中使用的匹配url-pattern

epe*_*leg 2 java servlets

假设我有一个Java servlet,我想用于两个(或更多)不同的url模式:

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/this/exact/path</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/that/prefix/path/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/yet/another/exact/path</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

MyServlet将被调用以下任何一个:

/this/exact/path
/yet/another/exact/path
/that/prefix/path/param1
/that/prefix/path/param2.html
Run Code Online (Sandbox Code Playgroud)

我想知道的是如何从我的代码中告诉我们匹配请求的路径是什么.(即如果请求是/myapp/yet/another/exact/path我想要获取字符串/yet/another/exact/path).

我想也应该有一种方法可以区分/ that /前缀/路径/以及与之匹配的内容*如果有人能告诉我应该怎么做,那将会很好.

我试过String path = req.getRequestURI()但它也返回/ myapp部分.

axt*_*avt 9

HttpServletRequest.getServletPath()返回不包括的URL模式/*,并HttpServletRequest.getPathInfo()返回匹配的部分/*(或null完全匹配).