March 8, 2021
Hot Topics:

User Code: Java Applet and JavaScript Communications

  • By Zhou Wu
  • Send Email »
  • More Articles »

This article introduces a means for communicating between a Java applet and an HTML page. (Of course, we'll not use LiveConnect, which can only be used for Netscape.)

The communications is reached by overwriting the toString() and showStatus() methods from the Applet class. These methods can be called by JavaScript's applet objects. This may be not very elegant, but it works.

The Java applet code is here.

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

public class TApplet extends Applet
      private TextArea paraArea = new TextArea(8,50);      
      private TextField info1 = new  TextField(20);
      private TextField info2 = new TextField(20);
      private String communication;
      private String getInfo;
      private String sendInfo;
      public void init()
         Panel p2 = new Panel();
         Panel p3 = new Panel();
         p3.add(new Label("Name:"));
         p3.add(new Label("Phone:"));
         p2.add("Center", p3);
         add("South",  p2);  

       // Method called by Java Script applet object to send infomation to the applet.
       public void showStatus(String s)
               StringTokenizer st = new StringTokenizer(s, "|");

        // Method called by Java Script applet object to get infomation to the applet.
       public String toString()
          getInfo ="Name:" +  info1.getText() + " |";
          getInfo += "Phone:" +  info2.getText() + " |";
          return getInfo;

Java Applet and HTML Page Communication



Download source = 1.5 K

About the Author

Zhou Wu works for AT&T Wireless Service, Inc. More information.

This article was originally published on June 22, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date