HTML to Applet and Applet to Applet Communication

Two separate examples given on HTML to Applet communication in 1st page and Applet to Applet communication in 3rd page.

HTML to Applet: Reading from HTML into Applet

Sometimes, it may be required to initialize some data while the applet object is being created. This data may be useful to give the properties to the applet object at the creation stage itself. Or, the programmer would like to pass data to the applet. The advantage of this approach is whenever data changes, we affect it in HTML file that does not require compilation. Whatever data is avaiable in the HTML file, at the time of object creation, it is read and put into the applet. This data is written within the <applet> tag. To read this data, we use the getParameter() method of Applet.

1. Example on HTML to Applet communication

In this program, two numbers are read from HTML file and their product is displayed.

File Name:

import java.awt.*;
import java.applet.*;
public class ReadingNumbers extends Applet  
  int fn, sn;
  public void init()                                              
    String str1 = getParameter("firstNum"); 
    String str2 = getParameter("secondNum"); 

    fn = Integer.parseInt(str1);
    sn = Integer.parseInt(str2);
  public void paint(Graphics g)
    g.drawString("Values:  " + fn + " and " + sn, 50 , 40 );
    g.drawString("Their Product:  " + fn *sn, 50, 70);
    g.drawString("way2java greets you all", 50, 100);

    showStatus("Values are  " + fn + " and " + sn + " product is  " + fn *sn);

File Name: ParamName.html


Applet to Applet

Screenshot of ParamName.html

<param name="firstNum" value="18">

The above statement is written within the <applet> tag. The name is used as the key to retrieve the value 18. The data comes in the form of name/value pairs. name is used to retrieve the value (like key/value pairs of Hashtable).

String str1 = getParameter("firstNum");

getParameter() method of Applet takes a string as parameter and returns a string. The parameter is the name of the name attribute and the return string is the value associated with the name in the <applet> tag.

The programmer should parse the return string as per his requirements. The output is displayed both on the applet window (with drawString() method) and the status bar with showStatus() method.

1 thought on “HTML to Applet and Applet to Applet Communication”

Leave a Comment

Your email address will not be published.