HTML to Applet and Applet to Applet Communication



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



Applet to Applet

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.

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

Leave a Comment

Your email address will not be published.