Recipe 17.2 Embedding an Applet in a JSP Using the HTML Converter


You want to use the Java Plug-in HTML Converter tool to generate the tags for embedding an applet.


Use the HTML Converter tool within htmlconverter.jar , which is located in the lib directory of the directory where you have the Java SDK installed.


A busy developer can let the Java Plug-in HTML Converter tool produce the HTML tags that are responsible for loading Java applets. The Java Plug-in is a Java-based tool that allows applets to be run in the Sun Microsystems Java 2 runtime environment, rather than within the web browser's Java runtime environment. The Java Plug-in is installed on your machine when you install the JRE, including the installation of the SDK.

The HTML Converter tool will convert a specified JSP file that contains an applet HTML tag, replacing the applet tag with a more complex tag collection that allows most browsers to load the Java applet. The Converter leaves the rest of your JSP code untouched; it only replaces the JSP's applet tag.

Here is how to use the HTML Converter tool:

  1. Write the JSP file, adding an applet tag. Example 17-3 shows a JSP that embeds a Clock.class applet reference. This JSP, rather redundantly, dynamically writes a time string beneath the applet. I included this code to show that the Converter does not change the JSP code; it just alters the applet tag template text included with the JSP.

    Example 17-3. A JSP with an applet tag
     <%@ taglib uri="" prefix="c" %> <jsp:useBean id="date" class="java.util.Date" /> <html> <head><title>A Clock in a JSP</title></head> <body> <h2>The time...</h2> <applet code="Clock.class" codebase="http://localhost:8080/home/applets"> </applet> <br /><c:out value="${date}"/> </body> </html> 
  2. Open a command-line window to the lib directory of your SDK installation, such as H:\j2sdk1.4.1_01\lib .

  3. Type java -jar htmlconverter.jar -gui . This command launches the Swing version of the HTML Converter tool. Figure 17-2 shows what the tool looks like.

Figure 17-2. The HTML Converter (GUI version)

The HTML Converter can also be run from the command line. See the Java Plug-in Developer Guide for supported options:

  1. If you want to choose a back-up folder where the tool saves the old JSP file (with the applet tag), use the HTML Converter GUI window to choose this folder.

  2. Click the "Convert . . . " button with the JSP file specified in the top text field, and the Converter will overwrite the original file with additional object and embed tags.

Example 17-4 shows the code that replaced the applet tag in Example 17-3 (in bold font), as well as the code that the converter tool did not modify.

Example 17-4. The object and embed tags produced by the HTML Converter
 <%@ taglib uri="" prefix="c" %> <jsp:useBean id="date" class="java.util.Date" /> <html> <head><title>A Clock in a JSP</title></head> <body> <h2>The time...</h2>  <!--"CONVERTED_APPLET"--> <!-- HTML CONVERTER --> <OBJECT      classid =      "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"     codebase =      ",4,0,0"     >     <PARAM NAME = CODE VALUE = "Clock.class" >     <PARAM NAME = CODEBASE VALUE = "http://localhost:8080/home/applets" >     <PARAM NAME = "type" VALUE = "application/x-java-applet;version=1.4">     <PARAM NAME = "scriptable" VALUE = "false">     <COMMENT>         <EMBED              type = "application/x-java-applet;version=1.4"              CODE = "Clock.class"             JAVA_CODEBASE = "http://localhost:8080/home/applets"               scriptable = false              pluginspage =         "">             <NOEMBED>                      </NOEMBED>         </EMBED>     </COMMENT> </OBJECT> <!-- <APPLET CODE = "Clock.class" JAVA_CODEBASE =     "http://localhost:8080/home/applets"> </APPLET> --> <!--"END_CONVERTED_APPLET"-->  <br /><c:out value="${date}"/> </body> </html> 

Users may have trouble loading the applet in their browsers if they have several installed versions of the Java Plug-in. This occurs when users steadily upgrade their JRE or Java SDK versions, which install the corresponding version of the Java Plug-in. The simplest solution in these cases is to uninstall the old Java Plug-ins.

See Also

The Java Plug-in technology page:; Recipe 17.1 on embedding a Java applet using the jsp:plugin standard JSP action.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: