How to Get the Local Area Network IP Address using jsp page?
I have my Application written in JSP and Servlet page. Whenever I try to track the us开发者_运维知识库er'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...
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 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.
精彩评论