Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest#getRemoteAddr() returns NULL

Tags:

java

servlets

Why does HttpServletRequest.getRemoteAddr() return null sometimes? For approximately 2 out of 10 identical requests is returns null for no apparent reason.

like image 879
milan Avatar asked Jan 20 '12 07:01

milan


People also ask

What is a HttpServletRequest?

The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

What is the use of HttpServletRequest and HttpServletResponse?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.

Is HttpServletRequest an interface?

Extends the ServletRequest interface to provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods ( doGet , doPost , etc). String identifier for Basic authentication.

What is the difference between HttpServletRequest and ServletRequest?

ServletRequest provides basic setter and getter methods for requesting a Servlet, but it doesn't specify how to communicate. HttpServletRequest extends the Interface with getters for HTTP-communication (which is of course the most common way for communicating since Servlets mostly generate HTML).


1 Answers

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.

like image 61
milan Avatar answered Oct 05 '22 22:10

milan