Why does HttpServletRequest.getRemoteAddr()
return null
sometimes? For approximately 2 out of 10 identical requests is returns null
for no apparent reason.
Asked
Active
Viewed 4,105 times
7
-
Read this thread - http://stackoverflow.com/questions/262887/why-httpservletrequest-getremoteaddr-doesnt-work-in-java-servlet – KV Prajapati Jan 20 '12 at 08:57
-
3@AVD Returning NULL is not mentioned there. – milan Jan 20 '12 at 09:51
-
I am having the same problem running JBoss AS 7.1. My server receives approx 9000 requests ~= 9GB of text in a short timespawn every day. During load testing, some hits to my webservice come up with a null on getRemoteAddr(). – Yablargo Feb 21 '13 at 18:37
3 Answers
6
Using Tomcat7. Apparently getRemoteAddr()
can return NULL
if the request has already been consumed, i.e. a response has been sent. It can even throw NPE sometimes. An error in my code in any case.

milan
- 2,355
- 2
- 23
- 38
1
Did you pass the request object to another thread to read it asynchronously? In some web container, request objects will be recycled/reset once the servlet thread is finished. Then you might get null
when request.getRemoteAddr()
is called.

Anderson
- 2,496
- 1
- 27
- 41
0
That would be either a bug or a misconfiguration in the specific servletcontainer or proxy used. Lookup which one you are using, check its version and check if this bug has been reported before to its maintainers and check if there is a newer version to which you can upgrade.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555