Issue
If I will execute request.getRemoteHost()
and request.getHeader("HOST")
on a Java server will these methods return the same values?
According to the documentation yes, but I will be happy for the confirmation:
href="https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/ServletRequest.html#getRemoteHost--" rel="nofollow noreferrer">https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/ServletRequest.html#getRemoteHost--
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
Solution
request.getRemoteHost()
: Returns the fully qualified name of the client that sent the request.So, If you access it via localhost you would get something like :
0:0:0:0:0:0:0:1
. If you access it via some other machine, it will return you the IP address of that machine.request.getHeader("HOST")
: Returns the value of the "Host" header in the request.So, If you are hitting an application url : https://www.test.com, then request.getHeader("Host") would return you
www.test.com
Answered By - Amit kumar
Answer Checked By - Gilberto Lyons (JavaFixing Admin)