简体   繁体   English

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

[英]How to Get the Local Area Network IP Address using jsp page?

I have my Application written in JSP and Servlet page. 我的应用程序用JSP和Servlet页面编写。 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. 每当我尝试跟踪用户的IP地址时,它都会返回计算机的全局IP地址(代理服务器地址),而不是该计算机的局域网IP地址。 So, how to get the LAN IP Address of the user's machine. 因此,如何获取用户计算机的LAN IP地址。 Please Guide me get out of this issue... 请引导我摆脱这个问题...

Try this for a local machine... 在本地计算机上尝试一下...

1- InetAddress thisIp = InetAddress.getLocalHost(); 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 结果的示例是abcNamePc/172.11.0.115 ,它将为您提供信息PC名称和IP

to get the IP of a remote machine, if you know the name of the machine use this 获取远程计算机的IP,如果您知道计算机的名称,请使用此名称

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

or 要么

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

gets the remote IP of the client. 获取客户端的远程IP。

this works. 这可行。

By necessity, your web server will only see the IP address of the machine that sent the request to you . 根据需要,您的Web服务器将仅看到向您发送请求的计算机的IP地址。 If a user is behind a proxy server, that will be the proxy server making the request on behalf of the user (hence "proxy" ). 如果用户在代理服务器后面,则该代理服务器将代表该用户发出请求(因此为“ 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. 代理可能会发送一个额外的HTTP标头,例如X-Forwarded-For ,在这种情况下,它会告诉您正在执行的是谁。 That IP address in turn may also be a proxy though, you can't know. 但是,您不知道该IP地址也可能是代理。 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. 您获得的唯一可验证IP地址是您的Web服务器从中接收请求并将其发送到的IP地址。

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. 如果您的计算机位于局域网内,则a)首先不会出现此问题,b)如果确实如此,则可以通过计算机名称查询某些内容。 That would heavily depend on the network infrastructure though and is not generalizable. 但是,这将严重依赖于网络基础架构,并且无法推广。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM