You want to use an applet on an intranet or the Internet, but it needs a modern JDK to run.
Use the Java Plug-in.
Sun's Java Plug-in allows your applet to run with a modern JDK even if the user has an ancient browser (Netscape 2, 3, or 4), or an anti-standard-Java browser (Internet Explorer might come to mind). For Netscape, the plug-in runs as a Netscape Plug-in. For Microsoft, the plug-in runs as an ActiveX control. The Java Plug-in was previously a separate download but is included in the Java Runtime Environment (JRE) in all modern JDK versions.
The HTML code needed to make a single applet runnable in either of those two modes rather boggles the mind. However, a convenient tool (which Sun provides for free) converts a plain applet tag into a hairy mess of HTML that is "bilingual": both of the major browsers interpret it correctly and do the right thing. Note that since browser plug-ins are platform-dependent, the Plug-in is platform-dependent. Sun provides versions for Solaris and Windows; other vendors provide it ported to various platforms. Learn more at Java's Plug-in page, http://java.sun.com/products/plugin/.
To try it out, I started with a simple JApplet subclass, the HelloApplet program from Recipe Recipe 25.8. Since this is a JApplet, it requires Swing support, which is not available in older Netscape versions or newer MSIE versions. Here are some screenshots, and the "before and after" versions of a simple HTML page with an applet tag run through the converter. Example 23-2 shows a simple applet HTML page.
Example 23-2. HelloApplet.html
<html> <title>Hello Applet</title> <body bgcolor="white"> <h1>Hello Applet</h1> <hr> <applet code=HelloApplet width=300 height=200> <param name="buttonlabel" value="Toggle Drawing"> </applet> <hr> </html>
When I run this under Netscape 4.x, it dies because Netscape 4 doesn't fully support Swing. So I need to convert it to use the Java Plug-in. Editing the HTML by hand is possible (there is a spec on the Java web site, http://java.sun.com), but messy. I decide to use the HTMLConverter instead. It pops up a simple dialog window (shown in Figure 23-2), in which I browse to the directory containing the HTML page. Note that the program will convert all the HTML files in a directory, so approach with caution if you have a lot of files. When I click on the Convert button, it chugs for a while and then pops up the window shown at the bottom of Figure 23-2 to show what it did.
Figure 23-2. HTML converter
By the time the HTMLConverter is finished, the once-simple HTML file is simple no more (although the original is saved in _BAK). See Example 23-3 for the finished version of the HTML.
Example 23-3. HTML converter output
Sun's documentation makes the amusing claim that "this may look complicated, but it's not really." Your mileage may vary; mine did. In fairness to Sun, if you use the simpler templates you do get simpler converted output. But because I believe in choice, I used the "Extended" template to get a version of the file that can be used in almost any browser. The converter thus outputs the OBJECT version of the Applet for MSIE and the EMBED version for Navigator; other browsers can use one or the other. Both versions are cleverly interwoven to appear as ignorable comments to the other. Figure 23-3 shows this page running under Netscape, and Figure 23-4 shows it under MSIE.
Figure 23-3. Applet working in Netscape using Java Plug-in
Figure 23-4. Applet working in Microsoft Internet Explorer using Java Plug-in