PreviousHomeNext Search

Including an Applet

You can include an applet or JavaBeans component in a JSP page by using the jsp:plugin element. This element generates HTML that contains the appropriate client-browser-dependent constructs (<object> or <embed>) that will result in the download of the Java Plug-in software (if required) and client-side component and subsequent execution of any client-side component. The syntax for the jsp:plugin element is as follows:

  { align="alignment" } 
  { archive="archiveList" } 
  { height="height" } 
  { hspace="hspace" } 
  { jreversion="jreversion" } 
  { name="componentName" } 
  { vspace="vspace" } 
  { width="width" } 
  { nspluginurl="url" } 
  { iepluginurl="url" } > 
  { <jsp:params> 
    { <jsp:param name="paramName" value= paramValue" /> }+
  </jsp:params> } 
  { <jsp:fallback> arbitrary_text </jsp:fallback> } 

The jsp:plugin tag is replaced by either an <object> or <embed> tag as appropriate for the requesting client. The attributes of the jsp:plugin tag provide configuration data for the presentation of the element as well as the version of the plug-in required. The nspluginurl and iepluginurl attributes specify the URL where the plug-in can be downloaded.

The jsp:param elements specify parameters to the applet or JavaBeans component. The jsp:fallback element indicates the content to be used by the client browser if the plug-in cannot be started (either because <object> or <embed> is not supported by the client or because of some other problem).

If the plug-in can start but the applet or JavaBeans component cannot be found or started, a plug-in-specific message will be presented to the user, most likely a pop-up window reporting a ClassNotFoundException. If the applet or JavaBeans component cannot be found, most likely it has been packaged incorrectly. Client-side classes such as applets must be packaged in the root of the Web module, not in the WEB-INF/classes directory. So, the DigitalClock applet contained in the bookstore2 application is packaged at the same level as the JSP pages.

The Duke's Bookstore page banner.jsp that creates the banner displays a dynamic digital clock generated by DigitalClock:

Duke's Bookstore with Applet

Figure 4-3 Duke's Bookstore with Applet

The jsp:plugin element used to download the applet follows:

  align="center" height="25" width="300"
<jsp:plugin type="applet" code="DigitalClock.class" 
codebase="/bookstore2" jreversion="1.4" align="center" 
height="25" width="300"
iepluginurl="http://java.sun.com/j2se/1.4/download.html" >  
    <jsp:param name="language"
      value="<%=request.getLocale().getLanguage()%>" />
    <jsp:param name="country"
      value="<%=request.getLocale().getCountry()%>" />
    <jsp:param name="bgcolor" value="FFFFFF" />
    <jsp:param name="fgcolor" value="CC0066" />
    <p>Unable to start plugin.</p>
PreviousHomeNext Search

All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.