Program to create an applet to accept a username in the form of parameter and print 'Hello '

You can also pass parameters to an applet the below program shows the same. User defined parameters can be passed to applet by using <PARAM…> tag. Each <PARAM…> tag has a name attribute such as color and value attribute such as red. Parameters are passed to an applet when it get loaded.

init( ) method in applet can be defined to get control of parameters defined in the <PARAM…> tag. This can be done using getParameter( ) method, whose one string argument represents the name of parameter and returns a string containing the value of that parameter.

As shown in below program, we have passed Username as paramaeter name and RAHUL as parameter value in <PARAM…> tag. After that in init( ) method we have collected that parameter using getParameter( ) method which return the value as a string. If the parameter value is null then it will display "Java" otherwise it will display "Hello RAHUL" on applet window.

import java.applet.Applet;
import java.awt.Graphics;

public class HelloJavaParam extends Applet{

 String msg="";
 public void init() 
  msg = getParameter("Username");
  if(msg == null) 
   msg = "Java";
  msg = "Hello " + msg; 
 public void paint(Graphics g)

<applet code="HelloJavaParam" width=200 height=200>
<param name="Username" value="RAHUL">



Post a Comment

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Program to input age from user and throw user-defined exception if entered age is negative

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.