[英]finding the matched url-pattern used in a multiple servlet-mapping scenario
假设我有一个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>
MyServlet将被调用以下任何一个:
/this/exact/path
/yet/another/exact/path
/that/prefix/path/param1
/that/prefix/path/param2.html
我想知道的是如何从我的代码中告诉我们匹配请求的路径是什么。 (即如果请求是/myapp/yet/another/exact/path
我想获得字符串/yet/another/exact/path
)。
我想也应该有一种方法可以区分/ that /前缀/路径/以及与之匹配的内容*如果有人能告诉我应该怎么做,那将会很好。
我尝试了String path = req.getRequestURI()
但它也返回了/ myapp部分。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.