Recipe 17.1 Embedding an Applet in a JSPUsing jsp:plugin


Problem

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

Solution

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

Discussion

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" /> <html> <head><title>A Clock in a JSP</title></head> <body> <h2>The time...</h2>  <jsp:plugin type="applet" code="Clock.class" codebase=     "http://localhost:8080/home/applets" jreversion="1.4.1"> <jsp:params>     <jsp:param name="scriptable" value="false"/> </jsp:params> <jsp:fallback> Sorry, we are unable to start the Java plugin <br /> </jsp:fallback> </jsp:plugin>  <br /><c:out value="${date}"/> </body> </html> 

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=     "http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#     Version=1,2,2,0"> <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"> <COMMENT> <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"/> <NOEMBED> Sorry, we are unable to start the Java plugin <br /> </NOEMBED> </COMMENT> </OBJECT> 

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

Figure 17-1. A JSP with an embedded applet
figs/jsjc_1701.gif

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.



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

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