October 22, 2018
Hot Topics:

Use the BodyTagSupport Class to Add More Flexibility to JSP Pages

  • August 20, 2001
  • By Piroz Mohseni
  • Send Email »
  • More Articles »

JSP tag libraries allow the developer to associate code to XML-like tags. Methods such as doStartTag() or doEndTag() are called as the JSP engine processes the page. There are times, however, when the "body" of the tag has some significance and needs to be captured or manipulated by the application.

Piroz Mohseni

For example, we may have a "headline" tag that delimits some text to be used as the header of an article. If the page needs to capture the headline itself and syndicate it, store in a database or change the way it is presented, then it needs to access the body of the tag. The BodyTagSupport class provides this capability. It is a subclass of TagSupport with additional methods for interacting with the body content. It does so by implementing the interface BodyTag.

This interface consists of three methods. The doInitBody() method prepares for evaluation of the body content. The doAfterBody() performs any actions needed after the evaluation of the body content. The setBodyContent() method is used to set the bodyContent property.

The BodyTagSupport class provides a method called getBodyContent() which is what you use to get a BodyContent object. Access to the tag body provides additional flexibility to a JSP programmer. Users familiar with HTML can insert specific data inside the JSP page (using custom tags). By accessing the tag body, your custom tags can extract and transform that data into a suitable format. For example, raw numerical data can be transformed into an HTML table. By providing various attributes for the tag, you can further customize the behavior of the tag.

The following listing shows a tag that takes numbers delimited by a comma and displays them in an HTML table. Each row consists of multiple numbers separated by a comma. A semicolon indicates end of a row. Using the StringTokenizer class, the doEndTag() method parses the numbers (which are inside the body of the tag) and produces an HTML table.

package stag;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

public class STag extends BodyTagSupport 
    protected String elemSep =  ",";
    protected String lineSep = ";";
    public int doEndTag() throws JspException 
       String bodyText = bodyContent.getString();
       try {
       catch (Exception e) {
       	throw new JspTagException(e.getMessage());
       StringBuffer tableOut = new StringBuffer(); 
       StringTokenizer bodyTk = new StringTokenizer(bodyText, lineSep, false);
       while (bodyTk.hasMoreTokens())
           String str = (String)bodyTk.nextToken();
           StringTokenizer token = new StringTokenizer(str, elemSep, false);
      try {
       catch (Exception e) {
       	throw new JspTagException(e.getMessage());
       return EVAL_PAGE;        

    public void release() 
       elemSep = ",";
       lineSep = ";";


Here is a JSP page using this tag:

<%@ taglib uri="/stag" prefix="tbl" %>

The output would be:


There are some drawbacks to this approach as well. The custom tag code must be prepared to handle any data entered by the user and perform error handling if the data is not correct. It is recommended that this approach be used in controlled situations where the number of JSP page authors is limited and they can be trained on the specifics of using the custom tags.

Another useful technique is to capture the body using the bodyContent.getString() method and then share it to other elements of the JSP page. This can be done via the setAttribute() method of javax.servlet.jsp.PageContext. By setting a global attribute value to the body content of a custom tag, you can make that content available to other tags in the page. This is a technique that can prove to be valuable when various tags from different libraries need to communicate and share information. Obviously, by introducing this coupling, the custom tags lose their independence, but you gain extra functionality.

Have you written a useful class and would like it to be featured as "Class of the Month?" Contact me at mohseni@bita-tech.com.

About the Author

Piroz Mohseni is president of Bita Technologies, focusing on business improvement through the effective use of technology. His areas of interest include enterprise Java, XML, and e-commerce applications.

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date