1

I am developing an application in local machine using tomcat 7 & servlet 3. In this I am trying to read the client address in Servlet to identify from where the request is coming using request.getRemoteAddr() which is always returning null.

I also tried as metioned here , but facing the same issue. Some where I read that using machine name instead of localhost will resolve the issue. I tried using machine name still the same issue.

Can anyone provide any help links or solution doc for the necessary configuration changes to retrieve the ip address ?

Community
  • 1
  • 1
Kiran
  • 183
  • 5
  • 19
  • When I tried request.getHeader("x-forwarded-for") , in tomcat it always returned null, but when I used the same code in Jboss AS7 it gave the value as expected when compared with chicken IP. As of now we have moved to JBoss , wondering if we have to work with Tomcat 7 what filters I need to enable as mentioned below. – Kiran Jul 27 '15 at 05:13
  • Have you resolved the issue? Can you please let me as I am also facing same issue. – Anu Jun 02 '20 at 05:12
  • Dont recall now, but you can try out these https://memorynotfound.com/client-ip-address-java/. 2) Also other way can you check if you can add some header tag in client (And ensure client pass that info) . 3) Also there are some log if I recall acess*.log which is configured in server.xml explore that too. – Kiran Jun 03 '20 at 15:16

2 Answers2

1

Check what headers you are receiving. Probably, the request is coming through Proxy where the X-Forward-For is present. In that case, you have to read X-Forwarded-For value.

req.getRemoteAddr() returns by reading the properties from opened socket connection. Let says, there is a Proxy in between, the Proxy is the immediate client for the server. It is the responsibility of the Proxy to send the address of the actual client in the form of X-Forwarded-For header.

By default, tomcat does not honor X-Forwarded-For value. But there is a filter does this job for you. This filter will return X-Forwarded-For value when you call req.getRemoteAddr();

https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/filters/RemoteIpFilter.html

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

One of the possibilities can be getRemoteAddr() can return NULL if the request has already been consumed, which means that a response has been sent. This has been noticed in Tomcat 7

Also as a fallback you can check the X-Forwarded-For header by calling getHeader("X-Forwarded-For") and see what IP does it return.

Mudassar
  • 3,135
  • 17
  • 22