'ServerName vs LocalName in ServletRequest

There are 2 attributes in ServletRequest and would like to understand the difference between the 2. According to definition in https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html,

getServerName(): Returns the host name of the server to which the request was sent.
getLocalName(): Returns the host name of the Internet Protocol (IP) interface on which the request was received.

I think both are the hostnames of same entity here.

  1. Is it not so?
  2. Also what is difference between server and IP interface?


Solution 1:[1]

They are not the same:

  • serverName is the name of the virtual host requested by the HTTP client (cf. Host header). This can be overridden by the proxyName value of the connector, the RemoteIpValve or the RewriteValve,
  • localName is obtained through a reverse lookup of the local IP (the details depend on the OS).

Basically always use serverName to generate URLs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1