Solved

How to get Local Hostname using Applet

Posted on 2000-03-05
2
1,096 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
[X]
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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
null output 3 43
SequenceInputStream example 3 22
Selenium docs api java index 3 68
glassfish admin console 1 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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