Solved

How to get Local Hostname using Applet

Posted on 2000-03-05
2
1,077 Views
Last Modified: 2008-02-01
How to get the local hostname info using applet and display them
the web page?

Thanks
0
Comment
Question by:saltacid
2 Comments
 
LVL 1

Accepted Solution

by:
jadrek85 earned 50 total points
ID: 2586592
Hi Saltacid,

Here is the code, which reads the host name and displays it in the applet. Either you can run this applet or call this applet in browser to display the host name of your machine.

// Starts Code

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
public class HostName extends JApplet {
  boolean isStandalone = false;
  JLabel lblName = new JLabel();
  JLabel lblHost = new JLabel();

  //Construct the applet
  public HostName() {
  }

  //Initialize the applet
  public void init() {
    try  {
      jbInit();
    }
    catch(Exception e)  {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {
    lblName.setFont(new java.awt.Font("Dialog", 0, 20));
    lblName.setBounds(new Rectangle(132, 59, 400, 48));
    this.setSize(new Dimension(559, 303));
    this.getContentPane().setLayout(null);
    lblHost.setFont(new java.awt.Font("Dialog", 0, 20));
    lblHost.setText("Host Name:");
    lblHost.setBounds(new Rectangle(24, 59, 113, 48));
    this.getContentPane().add(lblName, null);
    this.getContentPane().add(lblHost, null);
    String sAdd="";
    String Name="";
    try{
    sAdd=InetAddress.getLocalHost().getHostName();
    Name=InetAddress.getLocalHost().getHostAddress();
    }catch(Exception oExp){}
    lblName.setText(sAdd);
      System.out.println("Local Machine Host Name="+sAdd);
      System.out.println("Local Machine IP Address="+Name);
  }

  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }

  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }

  //Main method
  public static void main(String[] args) {
    HostName applet = new HostName();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    frame.setTitle("Applet Frame");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);

  }
  // static initializer for setting look & feel
  static {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {}
  }

}
   
// Ends Code

Good Luck,
Jadhv
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2586612
use InetAddress.getLocalHost().getHostName() to get the local host name
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now