Java 如何获取 servlet 所在的主机名(带端口)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2591135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:35:03  来源:igfitidea点击:

How can I get a the host name (with port) that a servlet is at

javaservletshostname

提问by Ankur

I thought ServletContext might provide a method. Does the getAttribute() method of ServletContext provide any help i.e. is there an attribute name (maybe "host", "port") that will be of help.

我认为 ServletContext 可能会提供一种方法。ServletContext 的 getAttribute() 方法是否提供任何帮助,即是否有帮助的属性名称(可能是“主机”、“端口”)。

The reason for this is I want my application to run wherever it is deployed, and at one point I have to allow a user to click a link that points to a location on the file server. Hence I need to reference by the host and port and cannot use an internal reference.

这样做的原因是我希望我的应用程序在它部署的任何地方运行,并且在某一时刻我必须允许用户单击指向文件服务器上某个位置的链接。因此我需要通过主机和端口进行引用,不能使用内部引用。

采纳答案by Everyone

ServletRequest.getServerName(...)
ServletRequest.getServerPort(...)

回答by Tom Jefferys

The ServletRequest object that has been passed to your doGet, or doPost method has getServerNameand getServerPortmethods that provide this information.

已传递到您的doGet或doPost方法的ServletRequest对象具有getServerNamegetServerPort提供此信息的方法。

eg

例如

public void doGet(ServletRequest request, ServletResponse response) {
    System.out.println("Host = " + request.getServerName());
    System.out.println("Port = " + request.getServerPort());
}

回答by user310791

I have found in my old project the string:

我在我的旧项目中找到了字符串:

request.getHeader("host").contains("xxx")

request.getHeader("host").contains("xxx")

maybe it is the solution?

也许这是解决方案?

回答by Fang Zhen

As others mentioned above, host and port can be retrieved through request. On the other hand, it is impossible for the ServletContext provide the info since java applications are unaware of your host environment. i.e., an application with context path "foo"(which could be retrieved by ServletContext#getContextPath()) could receive requests both from a http port 8080 and a https port 8043. Reference: https://web.archive.org/web/20120401225136/http://www.java.net:80/node/701934

如上所述,可以通过请求检索主机和端口。另一方面,ServletContext 不可能提供信息,因为 Java 应用程序不知道您的主机环境。即,具有上下文路径“foo”(可以通过 ServletContext#getContextPath() 检索)的应用程序可以从 http 端口 8080 和 https 端口 8043 接收请求。参考:https://web.archive.org/web /20120401225136/http://www.java.net:80/node/701934

回答by Amit G

@Everyone has a good answer. But taking scheme, server name and port then mergin them. There is a simpler way:

@每个人都有一个很好的答案。但是采用方案,服务器名称和端口然后将它们合并。有一个更简单的方法:

You can use HttpServletRequest.getRequestURLand HttpServletRequest.getRequestURI.

您可以使用HttpServletRequest.getRequestURLHttpServletRequest.getRequestURI

StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String host = url.substring(0, url.indexOf(uri)); //result