如何使用jsp页面获取局域网IP地址?

发布于 2024-11-08 01:17:14 字数 136 浏览 0 评论 0原文

我的应用程序是用 JSP 和 Servlet 页面编写的。每当我尝试跟踪用户的 IP 地址时,它都会返回计算机的全局 IP 地址(代理服务器地址),而不是该计算机的局域网 IP 地址。那么,如何获取用户机器的LAN IP地址。请指导我摆脱这个问题......

I have my Application written in JSP and Servlet page. Whenever I try to track the user's IP address it returns the Global IP Address of the machine (proxy server address), and not the Local area network IP Address of that machine. So, how to get the LAN IP Address of the user's machine. Please Guide me get out of this issue...

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

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

发布评论

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

评论(2

帥小哥 2024-11-15 01:17:14

在本地计算机上尝试此操作...

1- InetAddress thisIp = InetAddress.getLocalHost();
结果示例为 abcNamePc/172.11.0.115 它将为您提供这两个信息
pc 名称和 IP

来获取远程计算机的 IP,如果您知道计算机的名称,请使用此名称

InetAddress address = InetAddress.getByName("name of the machine");

String sIPAddress = request.getRemoteAddr();   //or getRemoteHost()

获取客户端的远程 IP。

这有效。

Try this for a local machine...

1- InetAddress thisIp = InetAddress.getLocalHost();
example of the result would be abcNamePc/172.11.0.115 it will give you the both info
pc name and IP

to get the IP of a remote machine, if you know the name of the machine use this

InetAddress address = InetAddress.getByName("name of the machine");

or

String sIPAddress = request.getRemoteAddr();   //or getRemoteHost()

gets the remote IP of the client.

this works.

久隐师 2024-11-15 01:17:14

根据需要,您的 Web 服务器只会看到向您发送请求的计算机的 IP 地址。如果用户位于代理服务器后面,则该代理服务器将代表用户发出请求(因此“代理”)。您无法进一步追溯请求的来源,因为它可能来自任何地方。只有代理服务器会知道,除非它告诉你,否则你不会知道。

代理可能发送一个额外的HTTP标头,例如X-Forwarded-For,在这种情况下,它会告诉您它代表谁进行操作。不过,该 IP 地址也可能是代理,但您无法知道。此外,此信息无法验证并且可能是伪造的,因此您无论如何都不应该依赖它。您获得的唯一可验证的 IP 地址是您的 Web 服务器接收请求并将响应发送到的地址。

例如,如果您的计算机位于 LAN 内,则 a) 一开始就不会遇到此问题,b) 如果出现,您可能能够通过计算机名称查询某些内容。但这在很大程度上取决于网络基础设施,并且不具有普遍性。

By necessity, your web server will only see the IP address of the machine that sent the request to you. If a user is behind a proxy server, that will be the proxy server making the request on behalf of the user (hence "proxy"). There's no way for you to trace the origin of the request any further back, since it might come from anywhere. Only the proxy server will know, and unless it tells you, you won't know.

The proxy may send an extra HTTP header like X-Forwarded-For, in which case it tells you whose behalf it's acting on. That IP address in turn may also be a proxy though, you can't know. Also, this information is unverifiable and can be faked, so you should not rely on it anyway. The only verifiable IP address you get is the one your web server received the request from and will send the response to.

If your machine was inside the LAN, you a) wouldn't have this problem to begin with and b) if you did, you may be able to query something by machine name, for example. That would heavily depend on the network infrastructure though and is not generalizable.

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