I have a requirement in which I need to display both IP address and hostname of the client machine. I have to use the chrome browser only for this activity. Below are the ways I tried.
Approach 1 - Using HttpServletRequest
HttpServletRequest request = RequestFilter.getRequest();
String ClientIP = request.getRemoteAddr();
String hostName = request.getRemoteHost();
logger.info("IP of the client:" + ClientIP);
logger.info("Host Name of the client :" + hostName);
With this approach request.getRemoteHost()
returns me the public ip as the hostname and not the Coumpter's name.
Approach 2 - Using Inet Address
InetAddress IP = InetAddress.getLocalHost();
String ClientIP = IP.getHostAddress();
String hostName = InetAddress.getLocalHost().getHostName();
logger.info("IP of client:"+ClientIP);
logger.info("Host Name of client:"+hostName);
This approach give me server's IP and hostname details. Not Clients.
I tried to get hostname by IP also using
InetAddress.getByName(request.getRemoteAddr()).getHostName()
But, this one too returns client's public IP and not the Hostname.
I tried using Javascript to get the hostname, but since I have to use only Chrome browser, I am unable to use ActiveXObject
.
On running hostname
command on cmd, I'm able to obtain the correct computer name which is my desired result.
Please suggest some way in which I can get the computer name as the hostname of the client. Please help, I'm stuck badly.