Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to get Local Hostname using Applet

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 54
String array comparison 4 38
Glassfish admin console not working 1 32
how to debug htl and js pages 8 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 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