Show TOC Start of Content Area

Procedure documentation Developing Classic 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.BodyTagSupport. BodyTagSupport implements the BodyTag interface and provides comfortable implementation for its methods.

       2.      Provide a public no-arguments constructor of the class.

Example

  public HelloWorldTag(){

     

  }

       3.      Create properties in JavaBeans style representing tag attributes.

Example

  private String bgcolor;

  private String title;

 

  public String getBgcolor() {

    return bgcolor;

  }

 

  public void setBgcolor(String bgcolor) {

    this.bgcolor = bgcolor;

  }

 

  public String getTitle() {

    return title;

  }

 

  public void setTitle(String title) {

    this.title = title;

  }

       4.      Implement the doStartTag() method. This method is called when the tag is first found in the JSP.

Example

  public int doStartTag() throws JspTagException { 

    //we need to perform some operations on the body of the tag,

    //so we return EVAL_BODY_BUFFERED

    //The JSP page implementation will create a    

    //javax.servlet.jsp.tagext.BodyContent

    //object where the body of the tag will be evaluated.

    return EVAL_BODY_BUFFERED;

  }

       5.      Implement the doEndTag() method.

Example

 For the HellowForldTag class, the doEndTag() method generates a table using the attributes for setting bgcolor and some text in the cells. 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 {

      String tableString = MessageFormat.format(

          "<table border=\"1\"><tr><td><b>{0}</b></td></tr>" +

          "<tr><td bgcolor=\"{1}\">",getTitle(), getBgcolor());

      pageContext.getOut().write(tableString);

      pageContext.getOut().write(getBodyContent().getString());

      pageContext.getOut().write("</td></tr></table>");

    } catch(IOException e) {     

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

    }

    return EVAL_PAGE;

  }

Example

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

package com.sap.demo.exampletags;

 

import java.io.IOException;

import java.text.MessageFormat;

import javax.servlet.jsp.JspTagException;

import javax.servlet.jsp.tagext.BodyTagSupport;

 

public class HelloWorldTag  extends BodyTagSupport{

 

  private static final long serialVersionUID = 956562171591119686L;

  private String bgcolor;

  private String title;

 

  public String getBgcolor() {

    return bgcolor;

  }

 

  public void setBgcolor(String bgcolor) {

    this.bgcolor = bgcolor;

  }

 

  public String getTitle() {

    return title;

  }

 

  public void setTitle(String title) {

    this.title = title;

  }

 

  public int doStartTag() throws JspTagException { 

    return EVAL_BODY_BUFFERED;

  }

 

 

  public int doEndTag() throws JspTagException {

    try {

      String tableString = MessageFormat.format(

          "<table border=\"1\"><tr><td><b>{0}</b></td></tr>" +

          "<tr><td bgcolor=\"{1}\">",getTitle(), getBgcolor());

      pageContext.getOut().write(tableString);

      pageContext.getOut().write(getBodyContent().getString());

      pageContext.getOut().write("</td></tr></table>");

    } catch(IOException e) {     

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

    }

    return EVAL_PAGE;

  }

}

 

 

 

End of Content Area