Solved

How to get Local Hostname using Applet

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 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

22 Experts available now in Live!

Get 1:1 Help Now