Client Machine Name or Host name ?

A

Anil G

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
 
R

Ryan Stewart

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.
 
R

Robert Olofsson

: 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.

I will assume that you talk about servlets or something similar here
(my guess is that request is a HttpServletRequest, should not matter
much).

Do the client support reverse DNS lookups? probably not and then it is
hard to do it. Also not that an IP sometimes maps to several
names. Some of the web host companies run a few thousand host names on
a single ip-address.

Why do you need the name of the computer?

If you want remoteUser to be set you have to ask the client to
authenticate itself. Read rfc 2616, 2617 for basic authentication. If
you run tomcat you can configure authentication against text
file/db/ldap, but you will have to read the tomcat manual to see how.

/robo
 
Joined
Jan 31, 2008
Messages
1
Reaction score
0
(1) Go to.... <apache-directory>/conf/server.xml

(2) Locate the following part:

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 has been changed to apache's port 80 -->
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

(3) Change enableLookups="true"

(4) Now, restart tomcat and reload the jsp containing getRemoteHost() method! Hope you should see the host name now. It may not work through proxy, so you can override proxy for local addresses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top