Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which case the getRemoteHost method returns an IP address instead of the hostname?

On a server of our private network we have an HttpServlet which is contacted by a PC of the same network.

We need to know the hostname of the client which contacts the server. To do this we call the getRemoteHost method of the HttpServletRequest.

Some times this method returns the PC name of the client (wanted behavior) and some other the method returns the IP address. (same client, same server, same private network)

The API says:

java.lang.String getRemoteHost()

Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the hostname (to improve performance), this method returns the dotted-string form of the IP address. For HTTP servlets, same as the value of the CGI variable REMOTE_HOST

Returns: a String containing the fully qualified name of the client

I see that for HTTP servlet that value is the same of the CGI variable REMOTE_HOST. What does it mean? Is it up to the server to decide to resolve the address or not? Is there a way to force this behavior?

like image 644
hijack Avatar asked Feb 13 '13 08:02

hijack


1 Answers

In Tomcat, for example, the connector has a setting "enableLookups" which is disabled by default for performance reasons. See http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

Other containers may have different methods of doing the same thing.

like image 141
bimsapi Avatar answered Sep 27 '22 17:09

bimsapi