Solved

How to get Local Hostname using Applet

Posted on 2000-03-05
2
1,088 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 33
Unhandled exception type Exception 18 31
Running JavaFX on JDeveloper 12C 1 32
String array comparison 4 34
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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