[ Team LiB ] Previous Section Next Section

Recipe 17.1 Embedding an Applet in a JSPUsing jsp:plugin


You want to use the jsp:plugin standard action to execute a Java applet with the Java Plug-in software.


Use the jsp:plugin action positioned in the area of a JSP where you want the applet to appear.


The JSP specification provides a standard action, jsp:plugin, which produces the object and embed tags that are designed to allow browsers to load a Java applet. The action will run the applet using Sun Microsystems's Java Plug-in or initiate the download of the Plug-in if the user has not yet installed the Plug-in.

The Java Plug-in is designed to execute an applet using Sun Microsystems's Java 2 Runtime Environment, rather than any Java runtime provided by the browser. The installation of the Java JRE or Software Development Kit automatically installs the Java Plug-in.

Use nested jsp:param elements to provide the applet with any necessary parameter and value pairs. The jsp:param elements must be nested within a single jsp:params element.

Example 17-1 shows a JSP file that uses jsp:plugin to embed an applet named Clock.class. In this case, the Clock.class file is located in the same directory as the JSP in Example 17-1.

This applet originates from Sun Microsystems's sample applets: http://java.sun.com/products/plugin/1.4.1/demos/plugin/applets/Clock/example1.html

Example 17-1. Embedding a Java applet with jsp:plugin
<%@ 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>

<jsp:plugin type="applet" code="Clock.class" codebase=
    "http://localhost:8080/home/applets" jreversion="1.4.1">

    <jsp:param name="scriptable" value="false"/>

Sorry, we are unable to start the Java plugin <br />


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

Users who have installed Internet Explorer for Windows depend on an HTML object tag to provide the direction for loading the applet. In browsers that support the Netscape-style plug-in, the HTML uses it's embed tag. The jsp:plugin standard action generates HTML that should work with both browser types (but you still should test the resulting JSP, of course).

Example 17-2 shows the HTML tags generated by the jsp:plugin action when the Internet Explorer 5.5 and the Netscape browsers request the JSP in Example 17-1.

Example 17-2. HTML tags generated by the jsp:plugin action for loading a Java applet
<OBJECT classid=
    clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 codebase=

<PARAM name="java_code" value="Clock.class">

<PARAM name="java_codebase" value="http://localhost:8080/home/applets">

<PARAM name="type" value="application/x-java-applet;version=1.4.1">

<PARAM name="scriptable" value="false">


<EMBED type="application/x-java-applet;version=1.4.1" pluginspage=
    "http://java.sun.com/products/plugin/" java_code=
    "Clock.class" java_codebase=
    "http://localhost:8080/home/applets" scriptable="false"/>

Sorry, we are unable to start the Java plugin <br />


Figure 17-1 shows the JSP with the embedded applet.

Figure 17-1. A JSP with an embedded applet

See Also

The Java Plug-in technology page: http://java.sun.com/products/plugin/; Recipe 17.2 on embedding an applet using the Sun Microsystems HTML Converter.

    [ Team LiB ] Previous Section Next Section