java 如何获取带有像 http://www.europewebsite.co.uk 这样的 URL 的服务器的主机名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13122000/
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 the hostname for a server with URL like http://www.europewebsite.co.uk?
提问by pushya
The project i am working on have multiple instances(i.e different website) running in single code base. Based on the URL we show corresponding website.
我正在处理的项目有多个实例(即不同的网站)在单个代码库中运行。我们根据 URL 显示相应的网站。
For example, if http://www.uswebsite.com/
the we show US website. and ifhttp://www.cawebsite.com/
will show Canadian website. The code that is written to detect this is
例如,如果http://www.uswebsite.com/
我们显示美国网站。如果http://www.cawebsite.com/
将显示加拿大网站。为检测这种情况而编写的代码是
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String server` = httpRequest.getServerName();
If request is from the http://www.uswebsite.com/
then according to above code String server = uswebsite
so we have additional code written to pop up the corresponding site
如果请求来自http://www.uswebsite.com/
然后根据上面的代码String server = uswebsite
所以我们编写了额外的代码来弹出相应的站点
we are now planning to include the European instance in the same code base and i see that the URL for the europe site is going to be like this http://www.europewebsite.co.uk/. With getServer() as above will it fetch String server=europewebsite
. By having the .co.uk at the end will the above code still get String server=europewebsite
or String server=europewebsite.co
Please advise as i can't test it out this in my local.
我们现在计划将欧洲实例包含在同一代码库中,我看到欧洲站点的 URL 将类似于http://www.europewebsite.co.uk/。使用上面的 getServer() 将 fetch String server=europewebsite
。通过将 .co.uk 放在最后,上面的代码仍然会得到String server=europewebsite
或者String server=europewebsite.co
请告知,因为我无法在本地进行测试。
回答by Stephen C
The wording of your question is rather confusing, so I'm assuming that you simply want to extract the hostname from a hierarchical URL string.
您问题的措辞相当混乱,因此我假设您只想从分层 URL 字符串中提取主机名。
The most reliable way to do it is to construct an instance of java.net.URL
or java.net.URI
for the url string, and use the relevant getter to extract the hostname.
最可靠的办法做到这一点是构造的一个实例,java.net.URL
或者java.net.URI
为URL字符串,并使用相应的getter提取的主机名。
On the other hand, if you are trying to detect the virtual hostname for the current request in a servlet, then your httpRequest.getServerName()
gives you that hostname as it appears in the first line of the HTTP request (unless something has rewritten it ...).
另一方面,如果您试图在 servlet 中检测当前请求的虚拟主机名,那么您将httpRequest.getServerName()
提供出现在 HTTP 请求第一行中的主机名(除非某些东西已经重写了它......)。
On the other hand, if you simply want to extract "europewebsite"
from a DNS name like "www.europewebsite.co.uk"
, there is no general solution. I'd recommend converting the DNS to all lowercase and doing a lookup in a table that you have pre-initialized with all of the variations that you are prepared to recognize.
另一方面,如果您只是想"europewebsite"
从像 那样的 DNS 名称中提取"www.europewebsite.co.uk"
,则没有通用的解决方案。我建议将 DNS 转换为所有小写字母,并在您已预先初始化为您准备识别的所有变体的表中进行查找。
回答by Satish Bellapu
Normally httpRequest.getServerName() will return hostname, in your case it is complete name ie. "europewebsite.co.uk".
通常 httpRequest.getServerName() 将返回主机名,在您的情况下它是完整的名称,即。“欧洲网站.co.uk”。