views:

280

answers:

2

How can I get client side information using either Javascript or Java Servlets?

Client side information such as client's computer name, its IP Address etc.

Thanks in advance.

+1  A: 

You can get some information from the HTTP request headers in a servlet, such as the user-agent (so that you knows which browser the client is using (or want to let us think it is using)) and the remote-addr (the client's IP address (or the proxy's one if the client is using it)).

String userAgent = request.getHeader("user-agent"); // Browser identifier.
String remoteAddr = request.getRemoteAddr(); // IP address.

You can't access system environment variables using Javascript. That would be a security hole. There are ways using ActiveX, but that works only on a certain webbrowser developed by a team in Redmond and still then, the client would need to lower its security settings to allow it to run. That's a big no-no.

The only way to get the computer name is to run a client application which is served by a webpage and let this client application sniff it and send it to the server side. For example a Java Applet using respectively System.getProperty("COMPUTERNAME") and java.net.URL. You however need to sign it, else it will prompt a security warning as well.

BalusC
thanks for d reply
A: 

Get user IP through JS

var ip = '<!--#echo var="REMOTE_ADDR"-->';

Although I'm not sure on the computer name, I presume it would have to involve ActiveX. It use to be possible via ActivexObject in IE. Unsure if its possible anymore, highly doubt it because its not secure in the slightest.

Russell Dias
That only works if the server in question supports serverside HTML includes. The construct as you displayed is IIS specific. I don't expect that the OP is running servlets on IIS. Further that ActiveX approach won't work on all other browsers than MSIE and even on MSIE it won't work by default, you have to lower the security settings.
BalusC
Yeah I just came across how you had to reduce the security to ridiculously low levels. So, just stick with Java Servlets.
Russell Dias