JSP Plugin Action

JSP Plugin Action Element

  • All the JSP action elements are used during the request processing phase, and not as the directive elements and script elements, which are used during the translation phase
  • All JSP action elements are specified in XML tags and can not be used in an XML directive as these have no end tag.
  • The JSP plugin will generate the HTML code to embed a Java object (OBJECT or EMBED) on the page. This is typically a Java applet, but can also bee a JavaBean component.
  • The attributes of the JSP plugin tag provide configuration data for the presentation of the tag.
  • Here is the notation for the JSP plugin action element:
    XML Description
    <jsp:plugin attributes > content </jsp:invoke> The body (content) can contain a <jsp:params> with one or more <jsp:param> which are passed to the object. The body (content) can also contain a <jsp:fallback> which indicates the content to be used by the client browser if the plugin cannot be started.
  • The following table lists the attribute(s) for the JSP plugin action element:
    Attribute Valid values Default Description
    type applet | bean n/a Identifies the type of the component. This attribute is required.
    code "classFileName" n/a The full name of the class including package notation and the .class extention. This attribute is required.
    codebase "classFileDirectoryName" "." The directory names where to find the class file.
    [align] "bottom|top|middle|left|right" bottom Positioning of the object
    [archive] "archiveURI" n/a Specifies a space-separated list of URLs indicating resources needed by the object.
    [name] "instanceName" n/a Name of the object when submitted as part of a html form.
    [height] "inPixels" n/a Indicates the maximum height in CSS pixels.
    [width] "inPixels" n/a Indicates the maximum width in CSS pixels.
    [hspace] "leftRightPixels" n/a Specifies the whitespace on left and right side of an object in CSS pixels.
    [vspace] "topBottomPixels" n/a Specifies the whitespace on top and bottom of an object in CSS pixels.
    [jreversion] "version number" 1.2 Identifies the spec version number of the JRE the component requires in order to operate.
    [nspluginurl] pluginURL n/a URLs where the Java plug-in can be downloaded for Netscape Navigator.
    [iepluginurl] pluginURL n/a URLs where the Java plug-in can be downloaded for Internet Explorer.

Example of using JSP plugin action element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

First we need a project with an applet.

If you like to participate in the review of this example you must first create a Java project in Netbeans (the project name is PluginActionApplet).

  • This project shall be a plugin for a Web project.
    This project shall contain an applet:
    package app;
    
    import java.awt.Font;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    public class InfoJApplet extends JApplet {
    
      private javax.swing.JButton button;
      final String html1 = "<html><body style='width: ";
      final String html2 = "px'>";
    
      @Override
      public void init() {
        setLayout(null);
        button = new javax.swing.JButton();
        button.setText("Show param.");
        button.addActionListener((java.awt.event.ActionEvent evt) -> {
          String firstName = getParameter("firstName");
          String lastName = getParameter("lastName");
          String str=String.format("Hello, my name is %s - %s  %s."
                  ,lastName, firstName, lastName);
          JLabel mylabel=new JLabel(html1 + "200" + html2 + str);
          mylabel.setFont(new Font("Arial", Font.BOLD, 20));
          JOptionPane.showMessageDialog(this, mylabel,
                  "Information",JOptionPane.INFORMATION_MESSAGE);
        });
        add(button);
        button.setBounds(50, 60, 130, 40);
      }
    }

    For those who participate in the review: create a JApplet class in Netbeans and replace generated java code with that shown above (the class file name is InfoJApplet and package should be app).

  • We expect that two parameters (firstName and lastName) are given inside the plugin action element, which we shall create in our web project.
  • For the applet project to function as a plugin we must sign the generated jar file and annotate it with an applet descriptor.
  • For those who participate in the review: sign the jar file Netbeans

Create the Web Project.

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is PluginAction).

  • This project shall contain an JSP to demonstrate the use of plugin action element:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
      <head>
        <meta http-equiv="Content-Type" 
              content="text/html; charset=UTF-8">
        <title>JSP Plugin action Demo</title>
      </head>
      <body>
    
        <jsp:plugin
          type="applet"
          code="app.InfoJApplet.class"
          codebase="/PluginAction"
          jreversion="1.8"
          archive="PluginActionApplet.jar"
          >
          <jsp:params>
            <jsp:param name="firstName" 
                       value="James" />
            <jsp:param name="lastName" 
                       value="Bond" />
          </jsp:params>
          <jsp:fallback>
            <p>Could not load the applet!</p>
          </jsp:fallback>
        </jsp:plugin>
    
      </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is index).

  • In the plugin action element we pass to parameters with the params action element and param action element.
  • These parameters will be parameters to the generated html Object tag.
  • The code is app.InfoJApplet.class which is found in the archive PluginActionApplet.jar.
  • We need to include the archive PluginActionApplet.jar.
  • For those who participate in the review: include java project in Netbeans

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>JavaCodes</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>JavaCodes</servlet-name>
            <url-pattern>/JavaCodes</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>JavaCodes</welcome-file>
        </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url JavaCodes. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /PluginAction) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/PluginAction</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
  • If you press the button the browser will display a dialog:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.