java 如何在 JSP 中获取完整 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27926854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get full URL in JSP
提问by goose84
How would I get the full URL of a JSP page.
我如何获得 JSP 页面的完整 URL。
For example the URL might be http://www.example.com/news.do/?language=nl&country=NL
例如,URL 可能是http://www.example.com/news.do/?language=nl&country=NL
If I do things like the following I always get news.jsp and not .do
如果我执行以下操作,我总是会得到 news.jsp 而不是 .do
out.print(request.getServletPath());
out.print(request.getRequestURI());
out.print(request.getRequest());
out.print(request.getContextPath());
out.print(request.getServletPath());
out.print(request.getRequestURI());
out.print(request.getRequest());
out.print(request.getContextPath());
回答by Manoj Kumar
Given URL = http:/localhost:8080/sample/url.jsp?id1=something&id2=something&id3=something
给定 URL = http://localhost:8080/sample/url.jsp?id1=something&id2=something&id3=something
request.getQueryString();
it returns id1=something&id2=something&id3=something
它返回 id1=something&id2=something&id3=something
回答by Bozho
You need to call request.getRequestURL()
:
您需要致电request.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.
重建客户端用于发出请求的 URL。返回的 URL 包含协议、服务器名称、端口号和服务器路径,但不包含查询字符串参数。
回答by Anis SAIED
/**
* Creates a server URL in the following format:
*
* <p><code>scheme://serverName:serverPort/contextPath/resourcePath</code></p>
*
* <p>
* If the scheme is 'http' and the server port is the standard for HTTP, which is port 80,
* the port will not be included in the resulting URL. If the scheme is 'https' and the server
* port is the standard for HTTPS, which is 443, the port will not be included in the resulting
* URL. If the port is non-standard for the scheme, the port will be included in the resulting URL.
* </p>
*
* @param request - a javax.servlet.http.HttpServletRequest from which the scheme, server name, port, and context path are derived.
* @param resourcePath - path to append to the end of server URL after scheme://serverName:serverPort/contextPath.
* If the path to append does not start with a forward slash, the method will add one to make the resulting URL proper.
*
* @return the generated URL string
* @author Cody Burleson (cody at base22 dot com), Base22, LLC.
*
*/
protected static String createURL(HttpServletRequest request, String resourcePath) {
int port = request.getServerPort();
StringBuilder result = new StringBuilder();
result.append(request.getScheme())
.append("://")
.append(request.getServerName());
if ( (request.getScheme().equals("http") && port != 80) || (request.getScheme().equals("https") && port != 443) ) {
result.append(':')
.append(port);
}
result.append(request.getContextPath());
if(resourcePath != null && resourcePath.length() > 0) {
if( ! resourcePath.startsWith("/")) {
result.append("/");
}
result.append(resourcePath);
}
return result.toString();
}