Anil G said:
Hi,
Is it possible to retrieve client's computer name or Host name instead of just IP?
Currently i am using following,
String remoteAddress = request.getRemoteAddr();
String remoteHost = request.getRemoteHost();
String remoteUser = request.getRemoteUser();
But this yeilds only IP Addresses and remoteUser as null.
Please advise,
Thanks,
Anil G
I assume you're talking HttpServletRequest here. Read your API's:
getRemoteAddr--Returns the Internet Protocol (IP) address of the client or
last proxy that sent the request.
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.
getRemoteUser--Returns the login of the user making this request, if the
user has been authenticated, or null if the user has not been authenticated.
So first notice that getRemoteAddr and getRemoteHost both may not get the
client's machine. If the request came through a proxy, it will get that.
Second, if getRemoteHost is giving an IP, it may not be able to get a host
name. Third, you can't get a user name if the server from which the request
is coming does not require login. That said, if you have an IP address, you
can attempt to get a host name like this:
String ip = "127.0.0.1";
String hostName = InetAddress.getByName(ip).getHostName(); // or
..getCanonicalHostName()
See
http://java.sun.com/j2se/1.4.1/docs/api/java/net/InetAddress.html for
more info on getHostName and such.