Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

trying to return the computername using applet

  Asked By: Kerri    Date: Jul 22    Category: Java    Views: 2877
  

I'm trying to return the computername to use in the page, in
JavaScript. It returns null, as its defined initially, but
of course I'm trying to ge the local computer's computername.

Here is the source to: ComputerName.class
---------------------------------------------------
import java.applet.Applet;
import java.awt.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ComputerName extends Applet {
public InetAddress getVariable() {
InetAddress YourAd = null;

try{

YourAd = InetAddress.getLocalHost();
}

catch(UnknownHostException nohost){}

return YourAd;
}
}
---------------------------------------------------

here is the html part related to the applet.
---------------------------------------------------
<applet code="ComputerName.class" width="100" height="100"></applet>

<script language="JavaScript">
function foo() {
with(document.applets[0]) {
var a = getVariable();
alert(a);
}
}

setTimeout("foo()",3000);
</script>
---------------------------------------------------

I get the alert saying null.
What is wrong?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Faith Hughes     Answered On: Jul 22

I run it in ms internet explorer 5.5 and it worked ok,
it return  an InetAddress object.
And if I call an getHostName() method:

alert(a.getHostName());

it return my pc´s name.

If it returns  null , maybe the applet  havn´t been
initialited yet.
Try put it in a body tag and in the function
window.onload() call to foo()
Check the jvm in your browser too.

This works for me:
-------------------------------------------
<html>
<body>
<applet code="ComputerName.class" width="100"
height="100"></applet>
</body>
</html>

<script language="JavaScript">

function window.onload(){
foo();
}

function foo() {
with(document.applets[0]) {
var a = getVariable();
alert(a.getHostName());
}
}
</script>
-------------------------------------------

 
Answer #2    Answered By: Dinh Tran     Answered On: Jul 22

I've tried it on another pc, and it works, kinda. Its returning
localhost/127.0.0.1
If i use a.getHostName()) inside my client javascript, I can get just
localhost, but that isn't my computername.

I understand what localhost is, and why that is referring to
127.0.0.1, so don't explain that old news to. ...but please explain
why I get localhost and not the actual computername, and how to get
the correct computername.

 
Answer #3    Answered By: Ann Evans     Answered On: Jul 22

Also,
instead of just:

catch(UnknownHostException nohost){}


make a print out, to see if you are not getting into the catch
for example:

try{
YourAd = InetAddress.getLocalHost();
System.out.println(" -- YourAd has been set to: " +
toString().YourAd );
// or just + YourAd, I don't know InetAddress Class.
}

catch(UnknownHostException nohost){
System.out.println(" -- Error: YourAd has not been set! --
" );
}


return YourAd;
}
}

I would suspect that if it works on someone else's system, then
the object of ComputerName class  is being created, fine, but for some
reason the YourAd is not set. I think...
Good luck. Will be interested to know how you made out.

 
Didn't find what you were looking for? Find more on trying to return the computername using applet Or get search suggestion and latest updates.




Tagged: