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

发布于 2024-08-27 13:45:45 字数 202 浏览 7 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

失去的东西太少 2024-09-03 13:45:46

我在旧项目中发现了字符串:

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

也许这是解决方案?

I have found in my old project the string:

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

maybe it is the solution?

三生殊途 2024-09-03 13:45:45
ServletRequest.getServerName(...)
ServletRequest.getServerPort(...)
ServletRequest.getServerName(...)
ServletRequest.getServerPort(...)
成熟的代价 2024-09-03 13:45:45

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

例如

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

The ServletRequest object that has been passed to your doGet, or doPost method has getServerName and getServerPort methods that provide this information.

eg

public void doGet(ServletRequest request, ServletResponse response) {
    System.out.println("Host = " + request.getServerName());
    System.out.println("Port = " + request.getServerPort());
}
云巢 2024-09-03 13:45:45

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

您可以使用 HttpServletRequest.getRequestURLHttpServletRequest.getRequestURI

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

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

You can use HttpServletRequest.getRequestURL and HttpServletRequest.getRequestURI.

StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String host = url.substring(0, url.indexOf(uri)); //result
悟红尘 2024-09-03 13:45:45

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文