DRAFT! This material is experimental and might have significant errors and omissions.
Feedback to Kathy Walrath

Providing Both 1.0.2 and 1.1 Applets

Some widely used browsers such as the 3.0 version of Netscape Navigator and the X.X version of Microsoft Explorer can't run applets that use the JDK1.1 API. Even when every browser sold supports 1.1, users will take a while to switch to the new browsers. Until everyone has switched, you'll probably want to supply 1.0.2-compliant applets.

But what if you want to use new 1.1 features? As long as you take care to provide backward compatibility, you can use 1.1 features and still provide applets that prevalent browsers can display. You can provide backward compatibility in one of three ways:

Two applets, one HTML page
With this approach, you provide separate 1.0 and 1.1 applets from the same page, using a hack that relies on pre-1.1 browsers not understanding the <APPLET> ARCHIVE attribute. This approach works wonderfully in the JDK Applet Viewer, HotJava, and Netscape Navigator 2.0. It sometimes seems to work in Netscape Navigator 3.0, but not always.
One applet (mixed code), one HTML page
With this approach, you write a single applet that encloses 1.1 API calls in try/catch statements. The code might look inelegant, but this approach will almost certainly work. Unfortunately, this approach doesn't let you take advantage of every permutation of every 1.1 feature (such as making your Applet subclass implement the ActionListener interface), since you can't put class and method definitions in try/catch statements.
Two applets, two HTML pages
With this approach, you provide two HTML pages, one with 1.0 applets and one with 1.1 applets. This approach gives the page's reader the responsibility for choosing the applet version.

How to Implement the Three Approaches

The rest of this document describes how each of the three approaches works, giving examples of each.

Two Applets, One HTML Page

Here's an applet that uses this approach:

Your browser is completely ignoring the <APPLET> tag!

If you see a rectangle just above these words, then the applet is running. The text within the rectangle tells you which version (1.0.2 or 1.1) of the applet is running. If you don't see a rectangle, then the applet isn't running, for some reason. You should see some text giving you more information.


Note: This section describes a hack. The hack depends upon only 1.1 browsers understanding the ARCHIVE attribute of the <APPLET> tag.

The beta versions of the JDK 1.1 Applet Viewer (and possibly some early versions of the HotJava browser) don't understand the ARCHIVE attribute. Instead of ARCHIVE, the beta Applet Viewer expects ARCHIVES (note the final S). The 1.1 FCS version of Applet Viewer does understand ARCHIVE.

Help! Does the ARCHIVE hack work for you? Tell me what browser you're using (including version #), what platform you're running it on (e.g. Windows or Solaris), and what text the applets on this page display. Thanks!


Here's the <APPLET> tag for the above applet:

<APPLET ARCHIVE = "../1.1/Version.jar" 
        CODEBASE = "1.0.2/" 
	CODE     = Version.class 
	WIDTH=400 HEIGHT=40
	ALT="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason."
>
Your browser is completely ignoring the &lt;APPLET&gt; tag!
</APPLET>
Here's the file hierarchy for the server that provides the above applet:
        index.html (this file)
           |
    +--------------+
    |              |
  1.0.2           1.1
    |              |
Version.class  Version.jar (JAR file that contains Version.class)

If the browser adheres to 1.1, it takes the version of the applet that's in the 1.1 directory. If the browser doesn't understand the ARCHIVE attribute of the APPLET tag, then it runs the version of the applet that's in the 1.0.2 directory. If the browser is like Netscape Navigator 3.0 and interprets the ARCHIVE attribute but can't understand 1.1, then you have trouble. You'll probably get a message complaining about the format of the archive file.

Here's the command that created the JAR file:

jar cf Version.jar Version.class

For the curious, here are links to the 1.0.2 Version.java and the 1.1 Version.java. The 1.1 version of this applet doesn't happen to use any interesting 1.1 features; it merely avoids using deprecated API by using the 1.1 equivalent. However, since its source code is separate from the 1.0.2 applet's source code, it could use 1.1 features such as inner classes.

One Applet (Mixed Code), One HTML Page

Here is an applet that contains both 1.1 and 1.0.2 code:

Your browser is completely ignoring the <APPLET> tag!

This applet uses code like the following:

try {
    d = getSize(); //1.1 code
    l.setText("You are using JDK release 1.1.");
    doLayout();    //1.1 code
} catch (NoSuchMethodError e) {
    d = size();    //1.0.2 code
    l.setText("You are using JDK release 1.0.2.");
    layout();      //1.0.2 code
}
As you can see above, calls to new 1.1 methods are inside a try statement. The equivalent 1.0.2 code is inside a catch statement. When the applet runs in a 1.0.2 browser and attempts to execute a 1.1 method (getSize), a NoSuchMethodError exception occurs. The applet stops executing the code in the try statement and executes the code in the catch statement instead.

Here's a link to the source code.

Two Applets, Two HTML Pages

If neither of the above approaches suits you, you can always provide two pages, one for each version of the applet. You'll have to judge whether this rather clunky approach will work for your intended audience.

For an example of this approach, see our sorting demo. Having a separate page for each version of the sorting demo is acceptable because the demo is an example for programmers.