General | Tuesday, November 24, 2009

Getting Applets To Talk To Each Other

Do you have more than one applet on a web page? Do you want the applets to share water cooler gossip? Ahem..ahem..
On a more serious note, do you want one applet to invoke methods or set variables of another applet?

Long long ago, you may have accomplished applet communication with the use of static variables. This method of sharing applet data is not fool proof. Different applets may run in different instances of the Java Virtual Machine software. This makes it impossible for an applet to see static variables set by another applet.

An applet can communicate with other applets by using JavaScript functions
in the parent web page. JavaScript functions enable communication between applets
by receiving messages from one applet and invoking methods of other applets. See
the following topics for more information about the interaction between Java code
and JavaScript code:

You should avoid using the following mechanisms to find other applets and
share data between applets:

  • Avoid using static variables to share data between applets.
  • Do not use the getApplet and getApplets
    methods of the
    AppletContext class to find other applets. These methods only find applets that are running
    in the same instance of the Java Runtime Environment software.

Applets must originate from the same directory on the server in order to
communicate with each other.

Consider two applets called Sender and Receiver. When a user clicks the button
to increment the counter, the Sender applet invokes a
JavaScript function to send a request to the Receiver applet. Upon receiving the
request, the Receiver applet increments a counter variable and displays the value
of the variable.

To enable communication with another applet, obtain a reference to an instance of the
netscape.javascript.JSObject class. Use this instance to invoke
JavaScript functions.
The Sender applet uses an instance of the
netscape.javascript.JSObject class to invoke a
JavaScript function called sendMsgToIncrementCounter.

try {
JSObject window = JSObject.getWindow(this);
} catch (JSException jse) {

Note: To compile Java code that has a reference to classes in the
netscape.javascript package, include
<your JDK path>/jre/lib/plugin.jar
in your classpath. At runtime, the Java Plug-in software automatically makes
these classes available to applets.

Write the JavaScript function that will receive requests from one applet and
invoke methods of another applet on the web page.
The sendMsgToIncrementCounter JavaScript function invokes
the Receiver applet's incrementCounter method.

function sendMsgToIncrementCounter() { receiver.incrementCounter();

Note that the JavaScript code uses the name receiver to obtain a
reference to the Receiver applet on the web page. This name should be the same as the
value of the id attribute that is specified when you deploy the
Receiver applet.

The Receiver applet's incrementCounter method is shown next.

public void incrementCounter() {
String text = " Current Value Of Counter: " + (new Integer(ctr)).toString();

Deploy the applets on the web page as shown in the following code snippet.
You can view the Sender and Receiver applets and associated JavaScript code in
AppletPage.html in this example's source code..

<!-- Sender Applet -->
<script src=""></script>
var attributes = { code:'Sender.class',
archive:'examples/dist/applet_SenderReceiver/applet_SenderReceiver.jar', width:300, height:50} ;
var parameters = {};
deployJava.runApplet(attributes, parameters, '1.6');
<!-- Receiver Applet -->
var attributes = { id:'receiver', code:'Receiver.class',
archive:'examples/dist/applet_SenderReceiver/applet_SenderReceiver.jar', width:300, height:50} ;
var parameters = {};
deployJava.runApplet(attributes, parameters, '1.6');

Download source code for the Sender Receiver Applets example to experiment further.

As always, please don't hesitate to send us your feedback!

-- Sowmya Kannan

Join the discussion

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha

Visit the Oracle Blog


Contact Us


Integrated Cloud Applications & Platform Services