getting Computer name in a network environment

I support a business application that uses an Applet to retrieve end-user's computer name at login time using Java Applet. Since Applet takes time to load, we need to find another way of retrieving coputer name. Our application consists of JSPs. I used
InetAddress.getLocalHost().getHostName()
to get the computer name, it worked in my local environemt where the client and host were the same machine, but when I deployed it on the server, I get back the server name. I need to find a way how can I retrieve cleint's comuter name without using APPLET.

Thank you
javaCaravan0Asked:
Who is Participating?
 
Pramod KumarConnect With a Mentor Commented:
Use below javacript code to get the details.

To get the data on server you may:
1) send it to server using ajax
2) fill the data in hidden input box of a form and once the user submit the form, the data will go on server.
<html>
<head>
<script>
function test()
{
try
{
var ax = new ActiveXObject("WScript.Network");
document.write('User: ' + ax.UserName + '<br />');
document.write('Computer: ' + ax.ComputerName + '<br />');
}
catch (e)
{
document.write('Permission to access computer name is denied' + '<br />');
}
}
</script>
</head>
<body onload=test()>
</body>
</html>

Open in new window

0
 
objectsConnect With a Mentor Commented:
you can use one of the follwoing

request.getRemoteAddr();
request.getRemoteHost();

but theres no guarantee that the browser will include that information in the request header
0
 
CEHJCommented:
>> I need to find a way how can I retrieve cleint's comuter name without using APPLET.

There isn't a way without using client-side code such as an applet
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
HegemonCommented:
If you need to know the name of a _Windows_ PC in your _local_ network, you can get the client's IP and query the name by executing command line tools like nbtstat -A <IP>. This can be executed programmatically and the output captured.
0
 
javaCaravan0Author Commented:
As per the comments from all participant, it seems that CEHJ is write, there is no way to get the computer name without the client-side code. The solution provided by pramodkrjsr is a cient side code which can retrieve the computer name without running applet. Thanks pramodkrjsr.
I have tested it in my local. My only concern is that whenever I run the code, I get the following in the IE:
"An ActiveX control on this page might be unsafe to interact with the other parts of the page. Do you want to allow this interaction?"

How can I get rid of this message so that all our end-users accessing the application will not see this message.

Thanks
0
 
objectsCommented:
>  it seems that CEHJ is write,

no he is wrong. You can get it from the request as I explained in my first comment
problem is that there is no guarantee of it being included in the quest (as also explained in my first comment)
0
 
CEHJCommented:
You won't get it from the server side. The best you're going to get it is the IP address
0
 
javaCaravan0Author Commented:
I have tested the solution provided by pramodkrjsr  in my local. My only concern is that whenever I run the code, I get the following in the IE:
"An ActiveX control on this page might be unsafe to interact with the other parts of the page. Do you want to allow this interaction?"

How can I get rid of this message so that all our end-users accessing the application will not see this message.

0
 
CEHJCommented:
Normally you can't get rid of such messages: they're by intent and are there as a security measure
0
 
HegemonCommented:
So the solution is to derive names from IPs using WINS or DNS as in my earlier comment.
0
 
CEHJCommented:
If the applet is served from your *own network* then a server side lookup on the ip address should work
0
 
javaCaravan0Author Commented:
The solution is to use client side AJAX and retrieve the name of the machine running under Win OS.
Hrhremon: My app. is web based JSPs deriven, how can I using thew command line tools in my web app to pull the computer name? (I don't want to use APPLET)
0
 
CEHJCommented:
*IS* the applet served from your own network?
0
 
objectsCommented:
once you have the ip address from the request you can use the InetAddress class

http://helpdesk.objects.com.au/java/lookup-hostname-by-ip-address
0
 
HegemonCommented:
Once the server hosting your application receives a request, your JSPs or Servlets should be able to get client's IP. Knowing this IP, a command line tool can be executed or DNS queried directly  - on the SERVER ! In other words, the server queries the network about what is known about this particular IP.
0
 
javaCaravan0Author Commented:
All,

1. I tried pramodkrjsr's solution, it works but I get the message as explained above, in order to use this solution, I need to find a way of not letting the message prompted, if anybody knows how to get rid of the message without changing the security setting, please let me know.

2. I tried the objects's solution but it didn't retrieve my computer name, instead it got server's IP address and server name!

I really need a solution. Please suggest some other solution if you all think that pramodkrjsr's or objects's solution can't be scuritinized to make that work.

Thanks
0
 
objectsCommented:
> 2. I tried the objects's solution but it didn't retrieve my computer name, instead it got server's IP address and server name!

sounds like you're checking the wrong request. you should be looking up the ip returned from the client. you will likely need to send a request using js that include the client ip
0
 
javaCaravan0Author Commented:
objects:

This is my code that I used to test on the server, it doesn't return my copmuter name (so it will not return our client's computer name too, because all will be accessing from the same server):

<%@ page language="java" import="java.net.InetAddress" %>
<%
String IpAddress =  request.getRemoteAddr();
InetAddress address = InetAddress.getByName(IpAddress);
String hostname = address.getHostName();
out.println("request.getRemoteAddr()="+IpAddress);
out.println("InetAddress.getByName(IpAddress)="+address);
out.println("address.getHostName()="+hostname);
%>

Please correct me if I have done any mistake in coding.
0
 
objectsCommented:
Is the remote address included in the request? you'll need to send a request using js to make sure its included.
0
 
CEHJCommented:
Try
String IpAddress =  request.getRemoteHost();

Open in new window

0
 
javaCaravan0Author Commented:
objects:
           Passing the IP address in JS and then using that IP address in the above code may work but how can I get the IP address using JavaScript? I think the problem will be solved if we could find a way of retrieving IP address using JS.

CEHJ: I used it in the above code but it returns server's IP address. I need a way of getting IP address using JS.
0
 
CEHJCommented:
>>CEHJ: I used it in the above code but it returns server's IP address.

It will give you the host address of the client. Unfortunately, because of the way networks work, it won't necessarily be the actual client box's ip - it could be a gateway address
0
 
objectsCommented:
>  but how can I get the IP address using JavaScript?

you don't. you need to get the browser to include as I mentioned earlier. Using a XMLHttpRequest has a better chance of doing that. Still no guarantees as I explained in my first comment

> CEHJ: I used it in the above code but it returns server's IP address. I need a way of getting IP address using JS.

And its already been discussed.
0
 
javaCaravan0Author Commented:
Object:

> you need to get the browser to include as I mentioned earlier. Using a XMLHttpRequest has a better chance of doing that. Still no guarantees as I explained in my first comment

  sorry but I don't understand what you mean by "you need to get the browser to include as I mentioned earlier".

> Using a XMLHttpRequest has a better chance of doing that.

How can I use XMLHttpRequest  to get the IP address. Do you mean using this: var ax = new ActiveXObject("WScript.Network");
In that case, as I have mentioned, I always get the message that I don't want.
If there is a way to capture the IP address of the machine, I can use that IP address in JSP to get the machine name. This is where I'm stuck.

What I have tested so far, does't work 100%.

1. using ActiveXObject("WScript.Network") to get the computer name always shows a warning message as explained earlier.

2.  if I pass IP address in "InetAddress address = InetAddress.getByName(IpAddress); " , I'm able to capture the coputer name, but to make this work, I need a way of gettting the IP address using Javascript.

This is very critical, If anyone knows how to do this, please let me know.




0
 
CEHJCommented:
>>but to make this work, I need a way of gettting the IP address using Javascript.

Why do you need to use javascript?
0
 
javaCaravan0Author Commented:
is ther anyother way? How can I get the IP address.
0
 
CEHJCommented:
To avoid going around in circles:

a. it's not possible to get the computer name client-side without running privileged code and that will be noticeable to the client. That means an applet or JavaScript
b. it can be possible to get the remote ip address on the server side using request.getRemoteHost
c. if the request is coming from the same network, the address obtained in b. should be resolvable to a name

Now, you said that the address in b. was that of the server. That is rather strange - unless of course, the request you inspected *did* come from the server ...
0
 
CEHJCommented:
>>unless of course, the request you inspected *did* come from the server ...

(Which would naturally happen if testing on the server itself rather than a separate client on your network)
0
 
javaCaravan0Author Commented:
I deployed the application on the server and tested it from my comuter.

Do u think this is an issue with JBOSS AP. Please refer to this site: (it is an issue with Oracle AS)
http://aboutoracle.blogspot.com/2006/11/tip-requestgetremoteaddr-and.html
0
 
CEHJCommented:
>>
uncomment the line :

UseWebCacheIp On
>>

Did you do that?
0
 
javaCaravan0Author Commented:
where can I find this line in JBOSS AS (what xml file do I have to look at?)
0
 
CEHJCommented:
That's to do with Oracle, not JBoss
0
 
HegemonCommented:
request.getRemoteAddr() should return client's IP unless the JBoss server is fronted by another web server, most likely Apache. Also see this discussion:

http://www.coderanch.com/t/445496/Tomcat/request-getRemoteAddr
0
 
CEHJCommented:
>> (it is an issue with Oracle AS)

Are you actually using that?
0
 
javaCaravan0Author Commented:
no, I'm using JBOSS AS
0
 
CEHJCommented:
I thought you were - since you posted a link about it ;). Just make sure that nothing along the same lines is happening in your case
0
 
objectsCommented:
> How can I use XMLHttpRequest  to get the IP address

http://www.jibbering.com/2002/4/httprequest.html
0
 
javaCaravan0Author Commented:
I'll be closing it by next week
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.