Monday, September 28, 2009

request.getRequestURL()和request.getRequestURI()有什么区别

更多精彩请到 http://www.139ya.com

Request.getRequestURL返回的是请求的全部,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
request.getRequestURI得到的是request URL的部分值,并且web容器没有decode过的
getRequestURL:
public 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.

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

getRequestURI:
public 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(javax.servlet.http.HttpServletRequest).
Returns:
a String containing the part of the URL from the protocol name up to the query string
See Also:
HttpUtils.getRequestURL(javax.servlet.http.HttpServletRequest)

No comments: