File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How can I resolve the IP address of a PC into the location of that PC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I resolve the IP address of a PC into the location of that PC" Watch "How can I resolve the IP address of a PC into the location of that PC" New topic
Author

How can I resolve the IP address of a PC into the location of that PC

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Hello everyone,
I'm currently building a website and I want to include an Applet that would display the visitors IP address, the name of thier PC and their locale (Where they are logging in from) and thier username on the PC machine they are logging in on. Now I have done the first two, but I am having problems with the last two and I need help here.
Now I achieved the first two using:
For the first: return InetAddress.getLocalHost().getHostAddress();
and return InetAddress.getLocalHost().getHostName(); for the second.
Does anyone know of a way to get the Locale of the user or to resolve the IP address into something useful, like the location in which the applet is running? Also, does anyone know how to get the username of the user?
I am entirely sure these things are possible, moreso when the applet runs completely on the client machine and the location of the computer is fed in at installation.
Any useful suggestions would be appreciated.

Thanks.


SCJP
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Originally posted by Femi Alla:
I want to include an Applet that would display the visitors IP address, the name of thier PC and their locale (Where they are logging in from) and thier username on the PC machine they are logging in on.

I wouldn't really favor an applet for doing these things, as they are best accomplished on the server side. Of course, that may not be an option if you are not allowed do server side code.
That being said, you can call Locale methods in your applet to get the Locale info,

According to the API for java.util.Locale, this will get you, "the current value of the default locale for this instance of the Java Virtual Machine" and "a name for the locale's country that is appropriate for display to the user" respectively.
Of course, this has nothing to do with the IP address that the HTTP request originates from, so it may not be what you are looking for.
If you want information that is derived from IP address lookups, this is best done on the server side, and then passed to the applet as a parameter. You won't know enough about the requestor's environment to be of much use on the client side, and the JVM security model greatly limits your options.
As to knowing the user's name (uid), I have to ask: what if they are using a Mac or a Win95/98/Me PC that doesn't require them to log in? There may not be a meaningful user ID available. And even if there is, the JVM security model should block your access to it (unless you are going to get into signed applets and all that).
Your best bet again is on the server side. If the user provides a user name or ID via a form, you can pass that as a parameter into your applet, save it in a cookie or in a Servlet environment as a Session property.
Hope this helps!
PCS


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
cer
Greenhorn

Joined: Sep 28, 2002
Posts: 8
i think to get the ip address ,we can use :
HttpRequet.getClient().getInetAddress().getHostAddress()
to get the client ip address
then with the method System.getProperty(''os.name'') and System.getProperty(''os.arch''),we can get the informations of the client os.


Nice to meet you here.<br />I will learn from you.
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Two things:
First, I don't think that "cer" is a valid username in this forum, and you will probably get scolded for it by one of the moderators.
Second, I don't believe that HttpRequest is a valid client side object (I know that it does not exist in the Java 1.4 API). You may be referring to a class called javax.servlet.http.HttpServletRequest, which is available in J2EE Servlet containers such as Tomcat.
If you wish to pursue the server-side option, here is (briefly) how you might approach it in a servlet:

In a JavaServer Page you have the implicit object named "request", which extends javax.servlet.ServletRequest, which is where the getRemoteAddress() method comes from.
You could have a line like,

PCS
[ November 06, 2002: Message edited by: Philip Shanks ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I resolve the IP address of a PC into the location of that PC