hostname-urgent

Hi pals, using applet iam trying to get hostname of machine running. can any one suggest me how to do in simple way.
[145 byte] By [advit] at [2007-9-26 2:51:50]
# 1
Did u try getRemoteHost() ?
b_babu at 2007-6-29 10:39:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi advit,

Here I am giving you the code, it will help you for your problem.

import java.applet.*;

import java.awt.*;

import java.net.*;

public class host extends Applet

{

String hostname;

public void init()

{

try

{

InetAddress localAddress = InetAddress.getLocalHost();

hostname = localAddress.getHostName();

} catch(Exception e)

{

hostname = "Unknown";

}

}

public void paint(Graphics g)

{

FontMetrics fm;

/* by using g .drawRect or square you define dimensions here,

after that getfonts using g.getfont() */

FontMetrics metrics = g.getFontMetrics ();

int messageWidth = metrics.stringWidth (hostname);

int startX = size().width/2 - messageWidth/2;

int startY = size().height/2 - fontDescent/2 + fontAscent/2;

g.drawString (hostname,startX, startY);

}

}

I hope this will help you out.

Regards,

Tirumalarao

Developer Technical Support,

Sun Microsystems,India.

rao_indts at 2007-6-29 10:39:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
the code which u have given getting the host name,what is the problem?
b_babu at 2007-6-29 10:39:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Not sure what exactly you want to do.

If you just want to know the client's hostname in a JSP page, use request.getRemoteHost().

If you want to be able to use it in an applet, then just pass it to the applet as a parameter:

<applet ...>

<param name='hostname' value='<%= request.getRemoteHost() %>'>

</applet>

mattbunch at 2007-6-29 10:39:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...