Why ServletRequest#getRemoteAddr() returns 127.0.0.1

When I found the reason why my Servlet container returns the “wrong” remote address I had to smite my forehead – long and hard. As so often the container is right and I was simply on the wrong track. It’s soooo obvious, Jesus…

If you access your application using the URL

http://localhost:port/

, you will get the remote address as 127.0.0.1 (local host address i.e. loop back address). Instead, try accessing the URL using your system/server name to get the desired output

http://systemname:port/

Yet another lesson learned I’ll never forget.

2 thoughts on “Why ServletRequest#getRemoteAddr() returns 127.0.0.1

  1. Hi
    I have the same problem and I couldn’t solve it with using your solution… 🙁
    I always get the 127.0.0.1 as remote address. Do you have any other idea?

  2. I think getRemoteAddr() returns the ip of the client, not the server. localhost/servername in your case denote the server, and the getRemoteAddr() should not be affected by this.

Leave a Reply