JSP & Inter-Applet Communication

JavaSketchpad supports inter-applet communication (IAC) with release DR2.2 and later. Inter-applet communication allows a single JavaSketch (an "instance" of the JavaSketchpad applet) to communicate with other instances of JavaSketchpad, with other applets, or with other mathematical JavaBeans. By assembling software components, you can create curricular activities that draw on the strengths of various technologies, of which JavaSketchpad is only one.

The primary purpose for JavaSketchpad's inter-applet communication facilities is to support JavaBean assembly in an "curriculum development" BeanBox. This approach will allow you to assemble components without writing code, and is part of our collaboration with the Educational Software Components of Tomorrow (ESCOT) Project. Descriptions of this functionality will appear at the ESCOT Project site as the technology matures.

In the meantime, if you write applets, you can use inter-applet functionality to control JavaSketches programmatically. This page describes JSP's inter-applet communication programming interface, and assumes you have familiarity with JSP's Construction grammar, with the Java language, with applet construction, and with HTML applet invocations.


What can you do with JavaSketchpad IAC?

Your applet can use IAC to control a JavaSketch--i. e. an instance of JSP running on the same page as your applet--remotely. In particular, an applet can:

The following examples demonstrate these capabilities by using external applets to monitor and control JavaSketches. (Note: These examples are extremely simple, for source-code clarity. This leaves them somewhat fragile. Be sure to wait until the JavaSketches have loaded before pressing any of the buttons in the sample applets.)



Example 1. Monitoring Measurements

The JavaSketch shown at right includes a (hidden) measurement which tracks angle A (an interior angle of the rhombus). A separate applet, below, "connects" to the JavaSketch and begins monitoring the angle. Whenever you change the angle in the JavaSketch (drag the red points), the monitoring applet receives notification and updates its display. You could use this form of IAC to track arbitrary numeric relationships in a JavaSketch, for plotting, data capture, or to determine when certain events have occurred that merit further action. (For instance, rather than displaying the measurement, your applet could simply wait for it to reach 90 degrees, and then take further action on the knowledge that the user had created a square.) Sorry, this page requires a Java-compatible web browser.

Example 2. Invoking Action Buttons


Sorry, this page requires a Java-compatible web browser.

This JavaSketch's Construction includes four hidden action buttons: two Movement buttons, which orient the sketch's 5-gon into regular pentagonal and regular pentagrammatic configurations; and a conventional pair of Show and Hide buttons controlling the visibility of a segment. (The segment in turn dictates the size of the regular polygon which the first two Movement buttons use as destinations.)

Two separate applets, each with a simple "push button" user interface, use IAC to communicate with this sketch. The first is responsible for coordinating movements, and you can try it here: . This applet sends requests to the JavaSketch to trigger one of its two Movement buttons. The second remote applet tracks whether the governing segment is visible, and is available here: .


How It Works

To control JavaSketches automatically, these example applets first locate the appropriate JSP applet on this web-page, and then invoke JSP-specific methods on that applet which support inter-applet communication.

Each instance of JSP on a web-page should be given a unique name (using the applet NAME parameter), so that your applet can locate it by name. Here's an example of a JSP instance applet named bob:

<APPLET CODE="GSP.class" CODEBASE="JSP" ARCHIVE="JSPDR3.JAR" WIDTH=186 HEIGHT=181 ALIGN=CENTER NAME="bob">

Your applet can use the getApplet() method of your applet's context to retrieve another (same-page) applet by name:

JSP_ExternIO jsp = (JSP_ExternIO) getAppletContext().getApplet("bob");

While getApplet() normally returns an object of type Applet, you'll want to cast it to type JSP_ExternIO so that you can then invoke JavaSketchpad-specific methods (for I/O with external callers; i. e. with your applet). In gain access to these methods, include the JSP_ExternIO class declaration in your applet's source project. This header file documents the various methods you can call on an instance of JSP that you've located with the above method.

Source code illustrating calls to JavaSketchpad's JSP_ExternIO methods is also available. Here's a link to the source for the applet in Example 1, above. And here's the source for the applet used twice in Example 2. (This applet allows itself to be configured by parameters in its HTML invocation. Even though the two applet buttons in Example 2 cause different interactions with the JSP instance, they both use the same controlling applet. View the source HTML of this page in order to see the separate HTML invocations of this applet.) Finally, Example 3 (not demonstrated above) offers a simple re-usable applet that creates a single push-button to trigger a single JSP actionButton.


Coding Notes

While in an ideal world, the Java specification claims that you can invoke a same-page applet's methods using the previously-described techniques, in practice you may find that not all browsers let you do so easily.

If you run into a ClassCastException when attempting to cast a getApplet() result to JSP_ExternIO, you may need to trick your browser into thinking that JSP and your applet (which is communicating with it) are separate GUIs associated with the same basic applet project. This means adding your applet's .class files to the JSP directory, and making all of your HTML <APPLET> invocations specify that CODEBASE="JSP". Use different CODE parameters to indicate whether to launch JavaSketchpad (CODE="GSP.class") or your own applet (CODE="yourApplet.class") in the different <APPLET> invocations. Note: When adding your applet's .class files to the JSP directory, don't add the JSP_ExternIO.class in your applet's class directory---JSP already contains one of its own. Also, either remove any .JAR or .ZIP archives (which browsers think of as "separate" applets), so that all that remains in the JSP directory is a collection of .class files (which your browser will think of as belonging to "one" applet) or repackage the traditional JSP .class files and your applets .classes into a new .JAR archive (referenced by an ARCHIVE parameter in your <APPLET> invocation).

Separately, if you're testing your multi-applet web-page from a local volume rather than an http server, you may run into Security Manager exceptions when you call getAppletContext(). Notably, Microsoft Internet Explorer (Mac and Windows versions) seems only to implement getAppletContext() and getApplet() correctly when the web-page and applets are located on a bonafide web server. You'll have to copy your test pages to a staging server in order to try them out, if Internet Explorer is your browser of choice.

Be sure to drop me a line if you do anything interesting with JavaSketchpad inter-applet communication!



return to the JavaSketchpad home