Way2Java

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: ReadingNumbers.java

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

             
             


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.


Getting address of Applet & HTML file

Applet programmer can get the address of an applet or HTML file using applet methods. The applet method getCodeBase() returns the address of the applet and getDocumentBase() returns the address of the HTML file. The return value of these methods is an object of URL from java.net package.

The next program uses these methods. The addresses are just retrieved and printed.

File: UsingBaseMethods.java

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

public class UsingBaseMethods extends Applet   
{
  String s1, s2;
  public void init()  
  {
    URL url1 = getCodeBase();            // to get applet address
    URL url2 = getDocumentBase();    // to get HTML address
    s1 = url1.toString();
    s2 = url2.toString();
  }
  public void paint(Graphics g)    
  {
    g.drawString( "Address of Applet file:  " + s1, 50, 40 );
    g.drawString( "Address of HTML file:  " + s2, 50, 70 );
  }
}

File: Base.html



Screenshot of Base.html

You can notice from the screenshot, the getCodeBase() returns the address of the applet file (does not include the name of the of the applet file) and getDocumentBase() returns the address of HTML file including the name of the HTML file.

Observe, the URL objects are taken as local and s1 and s2 are taken as instance (global) objects because s1 and s2 are required in paint() method. In init() method s1 and s2 are given values and used in paint() method.

2. Example on Applet to Applet communication

Browser permits the communication between applets. In the following program, two applets of ColorApplet are created, when a button of one applet is clicked, the background color of the other applet changes. To create two objects, we write two times the <applet> tag within the same <body> tag.

Applet file : ColorApplet.java

import java.applet.*;   
import java.awt.*;   
import java.awt.event.*;

public class ColorApplet extends Applet implements ActionListener   
{
     Button pinkBut, orangeBut, cyanBut;
     String s1= "";
     AppletContext ctx;
     ColorApplet ca;

     public void init()   
     {
	pinkBut = new Button("Pink");
	orangeBut = new Button("Orange");
	cyanBut = new Button("Cyan");

	pinkBut.addActionListener(this);
	orangeBut.addActionListener(this);
	cyanBut.addActionListener(this);

	add(pinkBut);   add(orangeBut);    add(cyanBut);
       }
       public void start()   
       {
	ctx = getAppletContext();
	ca = (ColorApplet) ctx.getApplet(getParameter("abc"));
       }  
       public void actionPerformed(ActionEvent e)   
       {
	String butLabel = e.getActionCommand();
		
	if(butLabel.equals("Pink"))             
		ca.setBackground(Color.pink);
	else if(butLabel.equals("Orange"))  
		ca.setBackground(Color.orange);
	else if(butLabel.equals("Cyan"))     
		ca.setBackground (Color.cyan);
        }
}                    

HTML file : ChangeColors.html


 
	                                



	




Screenshot of AppletToAppletDemo.html of Applet to Applet Communication

Check your browser can run this program. Some browsers do not support. Appletviewer will definitely run this program.

ctx = getAppletContext();

getAppletContext() method of Applet class returns an object of AppletContext(). The AppletContext() gets you the way to control the browser environment in which the applet or applets belonging to the same document are running.

ca = (ColorApplet) ctx.getApplet(getParameter("abc"));

getApplet() method of AppletContext returns the exact applet's execution environment which is passed as parameter. "abc" is the alias name of ColorApplet as included in the <applet> tag.

<applet> code="ColorApplet.class" width="250" height="250" name="first">
<param name="abc" value="second">
</applet>

Observe keenly the <applet> tag. Two objects of ColorApplet are created with two <applet> tags and the names are given as "first" and "second". In the first applet (whose name is "first"), we are referring the execution context of second applet (whose name is "second"). Similarly, the other way also, in the "second" applet where "second" applet refers the "first" applet. This is the place where actual link if provided between the two applets. Due to this link, if a button of one applet is clicked, the background of the other applet changes.

Note: To understand this program, knowledge of AWT Button is required.