admin管理员组

文章数量:1404923

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...

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...

Share Improve this question edited Sep 5, 2017 at 16:26 Sayan Pal 4,9567 gold badges47 silver badges84 bronze badges asked May 19, 2011 at 4:27 SaravananSaravanan 11.6k46 gold badges147 silver badges213 bronze badges 2
  • Is your web server outside that LAN? – deceze Commented May 19, 2011 at 4:31
  • @deceze:Could you please give your thoughts about for both cases?.But my Server is located in outside of LAN. What happen if my server is located inside the LAN? – Saravanan Commented May 19, 2011 at 4:38
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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 e 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.

本文标签: javaHow to Get the Local Area Network IP Address using jsp pageStack Overflow