ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button O'Reilly Book Excerpts: Java Servlet & JSP Cookbook

Cooking with Java Servlets & JSP

Related Reading

Java Servlet & JSP Cookbook
By Bruce W. Perry

by Bruce W. Perry

Editor's note: The Java Servlet & JSP Cookbook offers hundreds of tips and techniques that Java web developers can put to use every day. If you're thinking about adding this book to your Java library, here's a sneak peek at three recipes we've extracted from the book, which we think will make that purchasing decision an easy one. In these samples, learn how to use the Java Plug-in HTML Converter tool to generate the tags for embedding an applet, how to configure a javax.sql.DataSource for use in a servlet with the Tomcat web container, and how to use the JSTL's XML and XSLT-related tags. Enjoy.

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

Problem

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

Solution

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.

Discussion

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" />
    
    <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="$2004-02-25"/>
    
    </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
    Figure 17-2. The HTML Converter (GUI version)

    TIP: 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/.

  4. 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.

  5. 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" />

<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 = 
    "http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-
     i586.cab#Version=1,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 = 
       "http://java.sun.com/products/plugin/index.html#download">
            <NOEMBED>
            
        </NOEMBED>
        </EMBED>
    </COMMENT>
</OBJECT>

<!--
<APPLET CODE = "Clock.class" JAVA_CODEBASE =
    "http://localhost:8080/home/applets">
</APPLET>
-->


<!--"END_CONVERTED_APPLET"-->


<br /><c:out value="$2004-02-25"/>

</body>
</html>

WARNING: 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.

Pages: 1, 2, 3

Next Pagearrow