JSP Dobody Action.

JSP Dobody 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 dobody can only be used inside a tag file.
  • The JSP dobody invokes the body of the tag, 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 dobody action element:
    XML Description
    <jsp:dobody attributes > content </jsp:dobody> The content can only be one or more <jsp:attribute>
  • The following table lists the attribute(s) for the JSP dobody action element:
    Attribute Valid values Default Description
    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 dobody 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 DobodyAction).

JSP dobody 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, htmlFormat.tag:
    <%@ tag body-content="scriptless"  %>
    <%@ taglib prefix="c" 
               uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" 
               uri="http://java.sun.com/jsp/jstl/functions" %>
    
    
    <c:set var="keys" scope="request" 
           value="class,double,int,float,this,public
           ,private,package,return" ></c:set>
    
    <jsp:doBody  var="bodyRes"/>
    <c:forEach items="${keys}" var="key">
      <c:set var="formatted" 
             value="javaWord'>${key}</span>"/>
      <c:set var="bodyRes" 
             value='${fn:replace(bodyRes,key,formatted)}'/>
    </c:forEach>
    <div class="sourceCode">
      <pre><code >${bodyRes}</code></pre>
    </div>

    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 htmlFormat.tag and folder should be WEB-INF/tags).

  • We have to make the body content to be scriptless, and add two taglib directives to provide the required standard tag libraries.
  • JSP dobody action has a variable, "bodyRes" that will contain the body of the tag supplied by the JSP file.
  • With a foreach loop will each java key word be formatted if it is present in the content of what bodyRes refer to.
  • Finally, we print the contents with an EL.

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

  • <%@ page contentType="text/html" %> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> 
    <!DOCTYPE html>
    <html> 
      <head> 
        <title>Java source code</title> 
        <style>
          .sourceCode {
            font-family: "Courier New";
            font-size: 16px;
            padding: 2px 2px 2px 6px;
            background-color: #f8f8f8;
            word-wrap: break-word;
            border: 1px solid #b0b0b0;
            border-radius: 5px; 
          }
          .javaWord {
            color: #0033FF;
          }
          code {
            white-space: pre-wrap;
          }
        </style>
      </head> 
      <body  > 
        <div style="max-width:500px; ">
        <h1>Java source code</h1> 
        Here's a formatted java class: 
    
        <my:htmlFormat>package doc;
    
    public class Book {
    
      private String isbn_no;
      private String short_desc;
      private double price;
    
      public Book(String isbn_no, String short_desc, 
                  double price) {
        this.isbn_no = isbn_no;
        this.short_desc = short_desc;
        this.price = price;
      }
    
      public String getIsbn_no() {
        return isbn_no;
      }
    
      public String getShort_desc() {
        return short_desc;
      }
    
      public double getprice() {
        return price;
      }
    }</my:htmlFormat> 
        </div>
      </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 a tag with the prefix for tag-file folder and the tag-file name we supply all the text to the body part of the tag.

How does this work?

  • The body part of the <my:htmlFormat> will be transfered to the tag-file where some java key word will be reformatted.

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>Formater</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>Formater</servlet-name>
            <url-pattern>/Formater</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>Formater</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 Formater. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /DobodyAction) 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.