Show TOC Start of Content Area

Procedure documentation Implementing Tag Handlers  Locate the document in its SAP Library structure

Use

You implement the functions of your custom tags in tag handler classes. A tag handler class must implement the javax.servlet.jsp.tagext.Tag interface. Usually, this interface is appropriate for tags that do not process body. If you need your tag to support body, you must implement the javax.servlet.jsp.tagext.BodyTag interface (it is a subinterface of the Tag interface mentioned above). Those interfaces define certain methods that the Web Container calls when it encounters the tag in the JSP page.

Procedure

The procedure of developing a tag handler class consists of implementing the methods defined by the javax.servlet.jsp.tagext.Tag interface. We will give short description of each of the steps in the context of developing a simple HelloWorld tag. The result of the processing of this tag in the JSP page will be to add the Hello World string to the page.

...

       1.      Define the HelloWorldTag class that extends javax.servlet.jsp.tagext.Tag interface.

       2.      Provide a constructor of the HelloWorldTag class:

public HelloWorldTag(){

      super();

   }

       3.      Implement setPageContext() and setParent() methods that set the PageContext and the parent Tag object (if any) for your tag. In addition, implement a getParent() method that enables you to access a parent tag handler that encloses the current one (this is the cases when you use nested tags). Here are the implementations of those methods in HelloWorldTag class:

public void setPageContext(PageContext pageContext) {

      this.pageContext = pageContext;

   }

 

public void setParent(Tag parent) {

       this.parent = parent;

   }

 

public Tag getParent() {

      return parent;

   }

       4.      Implement the doStartTag() method. This method is called when the tag is first found in the JSP. Since the HelloWorldTag is a simple one and it does not have body, this method returns SKIP_BODY:

public int doStartTag() throws JspTagException {

      return SKIP_BODY;

   }

Note

When the method returns SKIP_BODY, no body is processed for that tag, even if a body is present. So that the body can be evaluated, the method must return EVAL_BODY_INCLUDE. In this case, if the body evaluation generates any result, it is included in the out implicit variable of the JSP.

       5.      Implement the doEndTag() method. This method writes the Hello World message to the output stream. It is called by the Web Container after the doStartTag() method returns. The return value of this method determines whether or not the rest of the body of the JSP page is evaluated further. The value of EVAL_PAGE enables further evaluation, whereas SKIP_PAGE completes the request to that JSP page. The doEndTag() method of the HelloWorldTag class is the following:

public int doEndTag() throws JspTagException {

     try {

     pageContext.getOut().write("Hello World!");

     } catch(IOException e) {

       throw new JspTagException("IO Error: " + e.getMessage());

     }

     return EVAL_PAGE;

   }

       6.      Define the release() method where you can release the various resources that your tag used. Since the HelloWorld tag is quite simple and does not use any external resources, this method has no body.

Example

The full source code of the HelloWorldTag tag handler is as follows:

Syntax

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import javax.servlet.jsp.JspTagException;

import javax.servlet.jsp.PageContext;

import javax.servlet.jsp.tagext.Tag;

 

import java.io.IOException;

 

 

public class HelloWorldTag implements Tag {

  

   private PageContext pageContext;

   private Tag parent;

  

   public HelloWorldTag(){

      super();

   }

  

   public void setPageContext(PageContext pageContext) {

      this.pageContext = pageContext;

   }

 

   public void setParent(Tag parent) {

     this.parent = parent;

   }

 

   public Tag getParent() {

      return parent;

   }

 

   public int doStartTag() throws JspTagException {

      return SKIP_BODY;

   }

  

   public int doEndTag() throws JspTagException {

     try {

     pageContext.getOut().write("Hello World!");

     } catch(IOException e) {

       throw new JspTagException("IO Error: " + e.getMessage());

     }

     return EVAL_PAGE;

   }

  

   public void release() {

     

   }

}

 

 

 

 

End of Content Area