Java 使用 JSTL 在 url 中获取域名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3806045/
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
Get domain name in url with JSTL?
提问by UpHelix
I am trying to get the domain name from the url with JSTL. The 2 methods I know return the wrong information. I need exactly what is in the URL.
我正在尝试使用 JSTL 从 url 获取域名。我知道的两种方法返回错误的信息。我需要的正是 URL 中的内容。
When I do:
当我做:
${pageContext.request.remoteHost}
I get my server's IP.
我得到了我的服务器的 IP。
When I do:
当我做:
${pageContext.request.serverName}
I normally get the right domain name, but on a server we have on amazon its returning "server1" instead of the correct domain name, probably because of the way it handles multiple domains.
我通常会得到正确的域名,但在亚马逊上的服务器上,它返回“server1”而不是正确的域名,可能是因为它处理多个域的方式。
Anyone know how I can get the current domain name in the URL?
任何人都知道如何在 URL 中获取当前域名?
I may need to get the URL and then parse it. How would I do that?
我可能需要获取 URL 然后解析它。我该怎么做?
采纳答案by matt b
You can use HttpServletRequest.getRequestUrl()
to:
您可以HttpServletRequest.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 包含协议、服务器名称、端口号和服务器路径,但不包含查询字符串参数。
this would return a String like Get domain name in url with JSTL?
这将返回一个字符串,如使用 JSTL 在 url 中获取域名?
It should then be trivial to parse this to find the string that comes after the scheme(http, https, etc) and before the requestURI.
然后解析它以找到在方案(http、https 等)之后和requestURI之前的字符串应该是微不足道的。
回答by Jigar Joshi
You can parse domain name from URL
您可以从 URL 解析域名
OR
或者
public static String getDomainName(String url)
{
URL u;
try {
u = new URL(url);
}
catch (Exception e) {
return "";
}
return u.getHost();
}
回答by BalusC
You should be using ServletRequest#getLocalName()
instead. It returns the real hostname of the server. The ServletRequest#getServerName()
indeed returns the value as set in Host
header.
你应该ServletRequest#getLocalName()
改用。它返回服务器的真实主机名。该ServletRequest#getServerName()
确实返回值作为集Host
头。
${pageContext.request.localName}
That's all. The solutions suggested in other answers are plain clumsy and hacky.
就这样。其他答案中建议的解决方案非常笨拙和笨拙。
By the way, the ServletRequest#getRemoteHost()
doesn't return the server's hostname, but the client's one (or the IP when the hostname is not immediately resolveable). It's obviously the same as the server's one whenever you run both the webserver and webbrowser at physically the same machine. If you're interested in the server's IP, use ServletRequest#getLocalAddr()
. The terms "local" and "remote" must be interpreted from server's perspective, not from the client's. It's after all the server there where all that Java code runs.
顺便说一句,ServletRequest#getRemoteHost()
不返回服务器的主机名,而是客户端的主机名(或当主机名不能立即解析时的 IP)。每当您在物理上在同一台机器上运行网络服务器和网络浏览器时,它显然与服务器的相同。如果您对服务器的 IP 感兴趣,请使用ServletRequest#getLocalAddr()
. 术语“本地”和“远程”必须从服务器的角度解释,而不是从客户端的角度解释。毕竟,所有 Java 代码都运行在服务器上。
回答by Siva Kumar Reddy G
${pageContext.request.contextPath}
${pageContext.request.contextPath}