«^»
4.4. Dealing with the different versions of the Java platform

Early versions of WWW browsers contain a Java interpreter that understands JDK 1.0.2, the version of Java that was prevalent at the time they were released. Each time a new version of a WWW browser was released, the latest version of the Java interpreter was included in the browser. So the Java interpreter of some WWW browsers understand JDK 1.1.x (although, unfortunately, with many versions of WWW browsers, some parts of JDK 1.1.x are missing).

During the years 1996-1998, this led to a chaotic state of affairs: some browsers would only execute applets coded with JDK 1.0.2, and other browsers only understood parts of JDK 1.1. It also seemed unlikely that any browsers would provide a Java interpreter for the Java 2 Platform. The best advice during this time was to write the Java source code for Java applets in terms of the language and the APIs of JDK 1.0.2, and to compile the source code with the JDK 1.0.2 compiler.

Of course, such an approach does not mean that you can reap the benefits of later versions of the Java Platform. For example, the way of handling events (such as the event of a user clicking a button) was improved between JDK 1.0 and JDK 1.1. And the Java 2 Platform brought the release of the Swing and Collection APIs.

The Java 2 Platform equivalent of the HelloApplet applet is the HelloJApplet applet that is given here:

0286: // A JDK 1.2 version of the HelloApplet applet.         // HelloJApplet.java
0287: // Barry Cornelius, 24th April 1999
0288: import java.awt.Graphics;
0289: import javax.swing.JApplet;
0290: public class HelloJApplet extends JApplet
0291: {
0292:    public void paint(final Graphics pGraphics)
0293:    {
0294:       pGraphics.drawString("Hello world", 50, 25);
0295:    }
0296: }

This class is derived from the JApplet class (from javax.swing), a class that is itself derived from java.applet.Applet. But, because it uses the features that were new with the Java 2 Platform, how can we run this applet?

Recognizing that browsers supporting different versions of Java interpreters was a major problem, Sun looked at how this problem might be overcome. They decided that it would be more flexible to provide a plug in containing a Java interpreter. (A plug-in is an additional piece of software that a browser can be configured to use. The advantage of using a plug-in is that a user can update it without having to update the browser.)

This plug-in is known as the Java Plug-in. (Note: it was previously called the Java Activator.)

The idea is that the applet is executed using the Java interpreter contained in this plug-in, and any Java interpreter contained in the WWW browser will be ignored. But, how can you arrange for your browser to use the plug in's Java interpreter rather than the browser's Java interpreter?

With early versions of the Java Plug-in, Sun suggested that developers of WWW pages that use Java applets code their HTML in such a way that the visitor to the WWW page is asked to download the plug-in to their computer if the plug-in appropriate to the version of Java required by the applet is not present on the computer.

Unfortunately, the way in which the HTML is written in order for this to happen depends on what browser is being used. For example, the HTML that is required for Netscape's Navigator is different from that that is needed for Microsoft's Internet Explorer. Although you could provide HTML that only works for one of these browsers, it is better for your HTML to allow any browser. So the reasonably simple APPLET tag of the file HelloApplet.html needs to be replaced by the HTML of the file HelloJApplet.html which is shown below.

This file contains a large number of difficult lines of HTML in order for it to work with both Navigator and Internet Explorer. Essentially, the lines immediately following the OBJECT tag are used by Internet Explorer, whereas those immediately following the EMBED tag are used by Navigator. There are details about what all this means at http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/using_tags.html.

On that WWW page, Sun give an even more complicated version that can deal with situations not catered for by the HTML given below. Even though it is complicated, once we have got it right the only parts that need to be changed are the two sets of references to the name of the .class file (HelloJApplet.class), the width (150) and the height (25).

With later versions of the Java Plug-in, when the user is installing the plug in, they can arrange for the plug-in's interpreter to be used when a WWW page is coded using an APPLET tag.

Obviously, you have no control over the users visiting your WWW pages: you do not know whether their browser has the Java Plug-in installed, nor whether it is a recent version of the Java Plug-in, nor whether they have configured the latter to understand the APPLET tag.

At the current time, probably the best advice is as follows:

0297: <HTML>
0298: <HEAD>
0299: <TITLE>The HelloJApplet Example</TITLE>
0300: </HEAD>
0301: <BODY>
0302: <P>
0303: Start.
0304: </P>
0305: <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
0306:   width="150" height="25"
0307:   codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0">
0308:   <PARAM NAME="code" VALUE="HelloJApplet.class">
0309:   <PARAM NAME="type" VALUE="application/x-java-applet;version=1.2">
0310:   <COMMENT>
0311:   <EMBED type="application/x-java-applet;version=1.2" 
0312:     width="150" height="25"
0313:     code="HelloJApplet.class"
0314:     pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html">
0315:    <NOEMBED>
0316:      </COMMENT>
0317:        No JDK 1.2 support which is what is needed for this applet
0318:    </NOEMBED>
0319:   </EMBED>
0320: </OBJECT>
0321: <P>
0322: Finish.
0323: </P>
0324: </BODY>
0325: </HTML>

Here is a link to a WWW page containing these HTML instructions: http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/simple/HelloJApplet.html.