[ Team LiB ] Previous Section Next Section

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="http://java.sun.com/jstl/core" prefix="c" %>
    <jsp:useBean id="date" class="java.util.Date" />
    <head><title>A Clock in a JSP</title></head>
    <h2>The time...</h2>
    <applet code="Clock.class" codebase="http://localhost:8080/home/applets">
    <br /><c:out value="${date}"/>
  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: http://java.sun.com/j2se/1.4.1/docs/guide/plugin/.

  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="http://java.sun.com/jstl/core" prefix="c" %>

<jsp:useBean id="date" class="java.util.Date" />

<head><title>A Clock in a JSP</title></head>
<h2>The time...</h2>

    classid = 

    codebase = 

    <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">

            type = "application/x-java-applet;version=1.4" 
            CODE = "Clock.class"
            JAVA_CODEBASE = "http://localhost:8080/home/applets"  
            scriptable = false 
            pluginspage = 



<br /><c:out value="${date}"/>


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: http://java.sun.com/products/plugin/; Recipe 17.1 on embedding a Java applet using the jsp:plugin standard JSP action.

    [ Team LiB ] Previous Section Next Section