如何在java中的jsp中获取实际的服务器名称和ip地址?

发布于 2024-12-27 07:01:15 字数 240 浏览 0 评论 0原文

我想在jsp中获取服务器的实际名称和IP地址。当我这样做时 request.getServerName()

它返回我本地主机。但我不希望名称为 localhost,而是机器的实际名称(就像在我的例子中它是 IBV-0076)。有没有一种方法可以获取正在处理请求的服务器的 IP 地址?

当我执行 request.getLocalAddr() 时,我得到的地址为 0.0.0.0 但我的服务器 ip 是 10.253.18.76

i want to get server actual name and ip address in jsp . when i do
request.getServerName()

it returns me localhost. But i dont want name as localhost but actual name of machine (like in my case it is IBV-0076). Is there a way i can get Ip address also of server which is processing the request?

When i do request.getLocalAddr() i get address as 0.0.0.0 but my server ip is 10.253.18.76

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

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

发布评论

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

评论(4

蓝颜夕 2025-01-03 07:01:15

我将使用 NetworkInterfaces 来获取有关服务器配置接口的信息。请参阅 Oracle 什么是网络接口?

I would use NetworkInterfaces to obtain information about servers configured interfaces. See Oracle What Is a NetworkInterface?

小嗷兮 2025-01-03 07:01:15

这还取决于您访问服务器的方式。
请使用实际的服务器IP地址,用于访问网页,然后查看结果。
这是因为如果您使用服务器的环回IP,该接口将绑定到“localhost”主机名。
当您在与服务器相同的计算机上测试 Web 应用程序时,通常会发生这种情况。

This also depends on how you have accessed the server.
Please use the acutal server IP address, which is used for accessing the webpage and then check the results.
This is because if you use the loopback IP of the server, the interface will be bound to "localhost" host name.
This usually happens when you are testing the webapp on the same machine as the server.

心碎无痕… 2025-01-03 07:01:15

对于 IP 地址,您可以为请求对象找到类似 getLocalAdrr() 的方法。
此外,您还可以使用 getRequestURL() 检查服务器名称的 URL。

如果您总是得到 0.0.0.0,请检查您服务器的网络配置。

For the IP-Address, you can find Methods like getLocalAdrr() for the Request-Object.
Also, you can check the URL for the Servername with getRequestURL().

If you always get 0.0.0.0, please check the Network-Configuration of your Server.

甜中书 2025-01-03 07:01:15

这是包含服务器名称和 IP 的示例。它是服务器在其端点上看到的 IP(可能因 NAT 而不同)和客户端使用的且可从客户端访问的 serverName。
${pageContext.request.localAddr}

here is an sample with Servername and IP. It is the IP the server see on its endpoint (maybe different because of NAT) and the serverName the client used and is accessible from the client.
${pageContext.request.localAddr}

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