I am trying to print the IP address of the logged-in user in my web application. If a user connects from another PC (within the same network, as the web application is running in my pc) using the IP address 192.168.10.120:8080/WebApplication
the code request.getRemoteAddr()
or request.getLocalAddr())
returns their IP address. However, when I log in from my pc, which runs the web application, I get this IP address 0:0:0:0:0:0:0:1
.
Why is this happening? And what's the difference between these commands (which one should I use)? Thank you in advance!
In your case, as you are trying to access it on your local machine,so it will return that value. But let one of your friend access it, and you will receive the expected result with getRemoteAddr
From the javadoc:
getRemoteAddr - Returns the IP address of the client or last proxy that sent the request
getLocalAddr - Returns the IP address of the interface on which the request was received.