JSP Invoke Action.

JSP Invoke 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 invoke can only be used inside a tag file.
  • The JSP invoke takes the name of an attribute that is a fragment, and invokes the fragment, sending the output of the result to the JspWriter, or to a scoped attribute that can be examined and manipulated.
  • Here is the notation for the JSP invoke action element:
    XML Description
    <jsp:invoke attributes > content </jsp:invoke> The content can only be one or more <jsp:attribute>
  • The following table lists the attribute(s) for the JSP invoke action element:
    Attribute Valid values Default Description
    fragment "fragmentName" n/a Fragment is required and the name of the fragment being invoked.
    var "varName" n/a Var is optional and is the name of the attribute that stores the result. Only one of var or varReader can be used.
    varReader "varReaderName" n/a VarReader is optional and is the name of the attribute of type java.io.Reader that stores the result. Only one of var or varReader can be used.
    scope "page|request|session|application" page Scope is optional and is the scope in which var or varReader is stored

Example of using JSP invoke 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).

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

JSP invoke action can only be used in tag-file.

  • All tag files are normally located under the WEB-INF/tags or sub-folders of this.
    Here is the tag-file, javaCodes.tag:
    <%@ tag body-content="empty" %> 
    <%@ attribute name="items" rtexprvalue="true" required="true" type="java.util.Map"%> 
    <%@ attribute name="var" rtexprvalue="false" required="true" %> 
    <%@ attribute name="even" fragment="true" required="true" %> 
    <%@ attribute name="odd" fragment="true" required="true" %> 
    <%@ variable name-from-attribute="var" alias="current" 
                 variable-class="java.lang.Object" scope="NESTED" %> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    
    <c:forEach items="${items}" varStatus="status" var="current"> 
      <c:choose> 
        <c:when test="${status.count % 2 == 0}"> 
         <%-- This only for dem. possible body of jsp:invoke --%>
          <jsp:invoke fragment="even" >
            <jsp:attribute name="var">data</jsp:attribute>
          </jsp:invoke> 
          <c:out value="${data}" escapeXml="false"/>
        </c:when> 
        <c:otherwise> 
          <jsp:invoke fragment="odd"  /> 
        </c:otherwise> 
      </c:choose> 
    
    </c:forEach> 

    For those who participate in the review: create a TAG file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is javaCodes.tag and folder should be WEB-INF/tags).

  • In this file, we expect to receive through an attribute all items we want to behandle. All the items are received through a java.util.Map.
  • We expect also two attribute (even and odd) which are defined as fragments. Both are required and must be implemented when this tag-file is used.
  • For each item we deal in the code we use alternately the two fragments with the help of JSP Invoke action (<jsp:invoke>).

We need a JSP file(s) to demonstrate use of the tag-file.

  • <%@ page contentType="text/html" import="java.util.HashMap" %> 
    <%@ taglib prefix="mt" tagdir="/WEB-INF/tags" %> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%
      HashMap values = new HashMap();
      values.put("class", "Java Class");
      values.put("public", "Public accessor");
      values.put("private", "Private accessor");
      values.put("interface", "Java Interface");
      values.put("double", "Double float varable");
      pageContext.setAttribute("values", values);
    %>
    <!DOCTYPE html>
    <html> 
      <head> 
        <title>Java key words</title> 
        <style>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          }
          table { 
            border-color: #aad5ff;
            border-style: solid;
            border-width: 0 0 1px 1px;
            border-spacing: 0;
            border-collapse: collapse;
          }      
          tr,th,td {
            border-color: #aad5ff;
            border-style: solid;
            padding: 2px;
            border-width: 1px 1px 0 0;
            vertical-align: central; 
          }
          table tr th {
            color: blue;
          }
        </style>    
      </head> 
      <body > 
        <h2>Java key words</h2> 
        <table  style="width:300px;"> 
          <tr class="gradientdown">
            <th>Number </th>
            <th>Key word </th>
            <th>Description</th>
          </tr>
          <mt:javaCodes items="${pageScope.values}" var="item" > 
            <jsp:attribute name="even"> 
              <c:set var="counter" value="${counter + 1}" /> 
              <tr style="background-color: #e0e6ff;">
                <td style="text-align: center;" >${counter} </td>
                <td>${item.key} </td>
                <td>${item.value}</td>
              </tr>
            </jsp:attribute> 
            <jsp:attribute name="odd"> 
              <c:set var="counter" value="${counter + 1}" /> 
              <tr style="background-color: lightyellow;">
                <td style="text-align: center;">${counter} </td>
                <td>${item.key} </td>
                <td>${item.value}</td>
              </tr>
            </jsp:attribute> 
          </mt:javaCodes> 
        </table> 
      </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).

  • We use the taglib directive at the start of the file to specify where to find the tag-file.
  • With the prefix for tag-file folder and the tag-file name we supply all the necessary attributes.
  • As the attributes "odd" and "even" are both defined to be fragmented we use <jsp:attribute > to specify these.

How does this work?

  • In a scriptlet scripting elements (<%) we have created a Map type list of Java key word with description we want to list in a table.
  • The Map type list of Java key word are given to the tab-file as an attribute (items)
  • We gets each element from the map in the var-attribute when the map is handled through the foreach loop in the tag-file (javaCodes.tag).
  • From each element from the map we can then output the key and the value, which is java key word and description.

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 /InvokeAction) for the application will in most cases be specified by a server vendor deployment descriptor.
    The browser will display:
© 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.