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 (Microsoft Explorer might come to mind). For Netscape, the plug-in runs as a Netscape Plug-in. For Microsoft, the plugin 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, there is a convenient tool (which Sun provides for free) that converts a plain applet tag into a hairy mess of HTML that is “bilingual”: both of the major browsers will 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 MS-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 Section 25.9. 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, it dies because Netscape 4 doesn’t fully support Swing. So I need to convert it to use the Java Plugin. 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 at the top of 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.
By the time the HTMLConverter is finished with it, the once-simple HTML file is simple no more. See Example 23-3 for the finished version.
Example 23-3. HTML converter output
<html> <title>Hello Applet</title> <body bgcolor="white"> <h1>Hello Applet</h1> <hr> <!--"CONVERTED_APPLET"--> <!-- CONVERTER VERSION 1.3 --> <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = 300 HEIGHT = 200 codebase="http://java.sun.com/products/plugin/1.3/jinstall- 13-win32.cab#Version=1,3,0,0"> <PARAM NAME = CODE VALUE = HelloApplet > <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <PARAM NAME="scriptable" VALUE="false"> <PARAM NAME = "buttonlabel" VALUE ="Toggle Drawing"> <COMMENT> <EMBED type="application/x-java-applet;version=1.3" CODE = HelloApplet WIDTH = 300 HEIGHT = 200 buttonlabel = "Toggle Drawing" scriptable=false pluginspage="http:// java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED></COMMENT> </NOEMBED></EMBED> </OBJECT> <!-- <APPLET CODE = HelloApplet WIDTH = 300 HEIGHT = 200> <PARAM NAME = "buttonlabel" VALUE ="Toggle Drawing"> </APPLET> --> <!--"END_CONVERTED_APPLET"--> <hr> </html>
Sun’s documentation makes the amusing claim that “this may look complicated, but it’s not really.” Your mileage may vary; mine did. The key point is that, since I used the default template, it built a version of the file that can be used with either MSIE (the OBJECT version) or Netscape (the EMBED version); both 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.
18.218.31.165