Recipe 23.6 Running an Applet with a Modern JDK


Problem

You want to use an applet on an intranet or the Internet, but it needs a modern JDK to run.

Solution

Use the Java Plug-in.

Discussion

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
figs/jcb2_2302.gif


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
<html> <head><title>Hello Applet</title></head> <body bgcolor="white"> <h1>Hello Applet</h1> <hr/> <!--"CONVERTED_APPLET"--> <!-- HTML CONVERTER --> <script language="JavaScript" type="text/javascript"><!--     var _info = navigator.userAgent;     var _ns = false;     var _ns6 = false;     var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info. indexOf("Windows 3.1") < 0); //--></script>     <comment>         <script language="JavaScript" type="text/javascript"><!--         var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") >  0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3. 5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0) || (_ info.indexOf("AIX") > 0) || (_info.indexOf("OS/2") > 0) || (_info.indexOf("IRIX") > 0)));         var _ns6 = ((_ns == true) && (_info.indexOf("Mozilla/5") >= 0)); //--></script>     </comment> <script language="JavaScript" type="text/javascript"><!--     if (_ie == true) document.writeln('<object class WIDTH = "300" HEIGHT = "200" codebase="http://java.sun.com/update/1.5.0/ jinstall-1_5-windows-i586.cab#Version=1,5,0,0"><noembed><xmp>');     else if (_ns == true && _ns6 == false) document.writeln('<embed ' +         'type="application/x-java-applet;version=1.5" \             CODE = "HelloApplet" \             WIDTH = "300" \             HEIGHT = "200" \             buttonlabel ="Toggle Drawing" ' +         'scriptable=false ' +         'pluginspage="http://java.sun.com/products/plugin/index.html#download"><noembed> <xmp>'); //--></script> <applet  CODE = "HelloApplet" WIDTH = "300" HEIGHT = "200"></xmp>     <PARAM NAME = CODE VALUE = "HelloApplet" >     <param name="type" value="application/x-java-applet;version=1.5">     <param name="scriptable" value="false">     <PARAM NAME = "buttonlabel" VALUE="Toggle Drawing"> </applet> </noembed> </embed> </object> <!-- <APPLET CODE = "HelloApplet" WIDTH = "300" HEIGHT = "200"> <PARAM NAME = "buttonlabel" VALUE="Toggle Drawing"> </APPLET> --> <!--"END_CONVERTED_APPLET"--> <hr/> </body> </html>

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
figs/jcb2_2303.gif


Figure 23-4. Applet working in Microsoft Internet Explorer using Java Plug-in
figs/jcb2_2304.gif




Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net