`
greenmoon
  • 浏览: 47054 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HttpServletRequest.getRequestUri不能获取forward之前的uri解决

阅读更多

当调用 RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)后,再调用javax.servlet.ServletRequest的getRequestUr方法得到的是forward后的uri,之前的uri得不到了。servlet规范明确规定了这种情况:getRequestURI

java.lang.String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:
First line of HTTP request	 Returned Value
POST /some/path.html HTTP/1.1		/some/path.html
GET http://foo.bar/a.html HTTP/1.0		/a.html
HEAD /xyz?a=b HTTP/1.1		/xyz
To reconstruct an URL with a scheme and host, use HttpUtils#getRequestURL.

Returns:
a String containing the part of the URL from the protocol name up to the query string
See Also:
HttpUtils#getRequestURL

 getRequestURL

java.lang.StringBuffer getRequestURL()
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
If this request has been forwarded using RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse), the server path in the reconstructed URL must reflect the path used to obtain the RequestDispatcher, and not the server path specified by the client.

Because this method returns a StringBuffer, not a string, you can modify the URL easily, for example, to append query parameters.

This method is useful for creating redirect messages and for reporting errors.

Returns:
a StringBuffer object containing the reconstructed URL

 

要想使用forward前的uri就需要费一翻脑筋了,好在spring UrlPathHelper提供了解决方案,public String getOriginatingRequestUri(HttpServletRequest request) 可以获取到之前的uri,UrlPathHelper是个好东西,还可以做很多事情。仔细研究一下这个方法:

/**
     * Return the request URI for the given request. If this is a forwarded request,
     * correctly resolves to the request URI of the original request.
     */
    public String getOriginatingRequestUri(HttpServletRequest request) {
        String uri = (String) request.getAttribute(WEBSPHERE_URI_ATTRIBUTE);
        if (uri == null) {
            uri = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
            if (uri == null) {
                uri = request.getRequestURI();
            }
        }
        return decodeAndCleanUriString(request, uri);
    }
 

针对websphere和其他的web容器有不同的方式,但是基础方式都是从request的属性中获取forward之前的uri,自从servlet2.4版本之后,request内部其实有一个属性存储了这个值,这个属性的名字是: /**

     * Special WebSphere request attribute, indicating the original request URI.
     * Preferable over the standard Servlet 2.4 forward attribute on WebSphere,
     * simply because we need the very first URI in the request forwarding chain.
     */
    private static final String WEBSPHERE_URI_ATTRIBUTE = "com.ibm.websphere.servlet.uri_non_decoded";

 

/**
   57   	 * Standard Servlet 2.4+ spec request attributes for forward URI and paths.
   58   	 * <p>If forwarded to via a RequestDispatcher, the current resource will see its
   59   	 * own URI and paths. The originating URI and paths are exposed as request attributes.
   60   	 */
   61   	public static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri";

 

参照代码:

http://www.docjar.com/html/api/org/springframework/web/util/WebUtils.java.html

http://www.docjar.com/html/api/org/springframework/web/util/WebUtils.java.html

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics