GuidesClass of the Month: JSP Tag Libraries and the TagSupport Class

Class of the Month: JSP Tag Libraries and the TagSupport Class

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

JSP Tag libraries provide a convenient framework for developing Web applications. They allow you to wrap custom functionality inside XML-like tags. By removing the “details” from the JSP page, it becomes easier to read. At the end of the day, however, JSP pages along with JavaBeans and tag libraries, get translated into servlets. So at runtime, it is the servlet that does the work.

Piroz Mohseni

With tag libraries, you can declare a custom tag and then associate some code with it and control when the code is invoked in relation to the opening or closing tags. The key class to make this work is TagSupport, which is part of the javax.servlet.jsp.tagext package. Note that this is not standard Java API. Most likely it will ship with your JSP/servlet engine, like Apache Tomcat.

A JSP page (regardless of whether it contains tag libraries or not) is processed from top to bottom. In a manner similar to the SAX API, various events are associated with this reading process. These “events” correspond to when the reader encounters a tag that is part of the tag library defined for the page. When the tag is first encountered, the doStartTag() method of TagSupport is called. By inheriting from TagSupport and overriding the doStartTag(), you can associate custom code and functionality to that event. As the reading process continues, it will eventually encounter the closing tag for the element and at that time it invokes the doEngTag() method.

When you call the doStartTag(), the method can return two values. One is SKIP_BODY, which instructs the reading process to ignore the body of the tag and move to the closing tag. The other value is EVAL_BODY_TAG, which instructs the reading process to process tags in the body and invoke the appropriate doStartTag() and doEndTag() method for each of them. When you call the doEndTag() method, it can return two values as well. One is SKIP_PAGE, which instructs the reading process to ignore the rest of the JSP page. The other is EVAL_PAGE, which has the opposite affect and indicates to the reading process that processing should continue immediately after the closing tag.

The following is a sample Tag handler that prints a message whenever the tag <Weather/> is encountered.

package demo;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class Weather extends TagSupport {
   public int doStartTag() {
      try {
        JspWriter out = pageContext.getOut();
        out.print("The weather is sunny and cloudy mixed with rain and
                      sunshine");

      } catch (IOException e) {
        System.out.println("Error " + e);
      }
      return (SKIP_BODY);
   }
}

If you need to perform certain actions based on the “body” of the tag (what is enclosed between the begin and closing tags), then you can use the BodyTagSupport class. Here, you have access to a few more methods like doAfterBody(), doInitBody(), and getBodyContent(). The doAfterBody() method is called after content body but before the closing tag. The getBodyContent() method encapsulates information about the tag body.

By understanding the TagSupport and BodyTagSupport classes, you can create powerful tag libraries for your Web applications.

From a life-cycle perspective, JSP pages are evaluated from top to bottom. Initially, setParent() and setPageContext() are called by the JSP engine. Then any “set” methods for attributes of the tag are called. Next, the following methods are invoked in this order: doStartTag(), setBodyContent(), doInitBody(), doAfterBody(), and doEndTag().

JSP tags can be nested and they can share objects/variables. By understanding the TagSupport and BodyTagSupport classes, you can create powerful tag libraries for your Web applications.

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.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories