[ Team LiB ] Previous Section Next Section

Using an Applet in Your Form

Applets are used to provide a rich user experience through a Web browser by allowing the user to interact with a GUI. Applets also make it possible to reorganize your application by encapsulating the presentation and performing validation of user input. They are often used to do client-side processing.

You can easily find dozens of Java books that tell you how to create an applet and put it in an HTML page. You may be familiar with the <applet> tag. The problem with the <applet> tag is that you don't have any control over the Java Virtual Machine that runs the applet. It's possible that the browser has a feature-incompatible virtual machine, or it may not have one installed at all.

Sun's solution to this issue is a part of the standard JRE (Java Runtime Environment) called the Java Plug-In. The <embed> tag in Netscape Navigator and the <object> tag in Internet Explorer let you force the browser to run your applet with Sun's VM. Better yet, you can use the most recent version of the VM, in case you need some of the newer features.

The JSP environment provides a special tag for embedding an applet in a Web page. The tag automatically detects the browser type and inserts the appropriate HTML tag (either <embed> or <object>) in the output. The JSP tag is called <jsp:plugin>. Listing 10.12 shows an example usage of <jsp:plugin>.

Listing 10.12 Source Code for ShowApplet.jsp
<html>
<body>
Here is the applet:
<br>

<jsp:plugin type="applet" code="examples.SwingApplet" codebase="."
    width="500" height="400">
    <jsp:fallback>
        <p>Unable to use Java Plugin</p>
    </jsp:fallback>
</jsp:plugin>

</body>
</html>

The text inside the <jsp:fallback> tag is displayed when the browser can't run the Java Plug-In, either because the browser isn't capable or there are problems loading it. If you need to pass parameters to the applet, you can use the <jsp:params> tag, which encodes a number of <jsp:param> tags. For example:


<jsp:params>
    <jsp:param name="myParam1" value="param1Value"/>
    <jsp:param name="myParam1" value="param1Value"/>
</jsp:params>

The <jsp:params> tag should be enclosed within the <jsp:plugin> in the same way that <jsp:fallback> is enclosed in Listing 10.12.

Listing 10.13 shows the generated HTML code when the ShowParams.jsp applet is run from within Internet Explorer.

Listing 10.13 ShowApplet.jsp Generated HTML for Internet Explorer
<html>
<body>
Here is the applet:
<br>

<object classid=clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 width="500" height="400"
graphics/ccc.gif 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="examples.SwingApplet">
<param name="java_codebase" value=".">
<param name="type" value="application/x-java-applet;">
<comment>
<embed type="application/x-java-applet;" width="500" height="400" pluginspage="http://java
graphics/ccc.gif.sun.com/products/plugin/" java_code="examples.SwingApplet" java_codebase="."/>
<noembed>

        <p>Unable to use Java Plugin</p>

</noembed>
</comment>
</object>


</body>
</html>

The <jsp:plugin> tag accepts most of the common <applet> attributes, such as code, codebase, archive, width, and height.

    [ Team LiB ] Previous Section Next Section