如何使用jsp页面获取局域网IP地址?
我的应用程序是用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在本地计算机上尝试此操作...
1-
InetAddress thisIp = InetAddress.getLocalHost();
结果示例为
abcNamePc/172.11.0.115
它将为您提供这两个信息pc 名称和 IP
来获取远程计算机的 IP,如果您知道计算机的名称,请使用此名称
或
获取客户端的远程 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 infopc name and IP
to get the IP of a remote machine, if you know the name of the machine use this
or
gets the remote IP of the client.
this works.
根据需要,您的 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.