Java 查找多servlet映射场景中使用的匹配url模式
假设我有一个Java servlet,我想用于两个(或更多)不同的url模式:Java 查找多servlet映射场景中使用的匹配url模式,java,servlets,Java,Servlets,假设我有一个Java servlet,我想用于两个(或更多)不同的url模式: <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/this/exact/path</url-pattern> </servlet-mapping> <servlet-mapping> <servlet
<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>
我想知道的是,如何从代码内部判断请求匹配的路径。(即,如果请求是向/myapp/yet/other/exact/path
发出的,我想获取字符串/yet/other/exact/path
)
我还想应该有一种方法来区分/that/prefix/path/和曾经匹配过的*如果有人能告诉我应该怎么做就好了
我尝试了String path=req.getRequestURI()
,但它也返回了/myapp部分。您应该使用:
/**
*
* Returns any extra path information associated with
* the URL the client sent when it made this request.
* The extra path information follows the servlet path
* but precedes the query string and will start with
* a "/" character.
*
* <p>This method returns <code>null</code> if there
* was no extra path information.
*
* <p>Same as the value of the CGI variable PATH_INFO.
*
*
* @return a <code>String</code>, decoded by the
* web container, specifying
* extra path information that comes
* after the servlet path but before
* the query string in the request URL;
* or <code>null</code> if the URL does not have
* any extra path information
*
*/
public String getPathInfo();
HttpServletRequest.getServletPath()
返回URL模式,不包括/*
,而HttpServletRequest.getPathInfo()
返回与/*
匹配的部分(或null
用于精确匹配)
/**
*
* Returns any extra path information associated with
* the URL the client sent when it made this request.
* The extra path information follows the servlet path
* but precedes the query string and will start with
* a "/" character.
*
* <p>This method returns <code>null</code> if there
* was no extra path information.
*
* <p>Same as the value of the CGI variable PATH_INFO.
*
*
* @return a <code>String</code>, decoded by the
* web container, specifying
* extra path information that comes
* after the servlet path but before
* the query string in the request URL;
* or <code>null</code> if the URL does not have
* any extra path information
*
*/
public String getPathInfo();