TagSupport class of Tag API

In this post, you will learn:

  • What is the role TagSupport class in custom tags?
  • How to define a custom tag class using TagSupport?

The TagSupport class:

TagSupport is a helper class provided by Tag API. It implements IterationTag interface which is a sub interface of Tag. It is used as a super class by the custom tag classes. As an application programmer defines a Servlet by extending HttpServlet class similarly he/she can define a Tag by extending the TagSupport class. Unlike HttpServlet, the TagSupport class is not abstract. It has following implementation:

package javax.servlet.jsp.tagext;

public class TagSupport implements IterationTag 
{
//Data Member to store the reference of the PageContext object
protected PageContext pageContext;

//Data Member to store the reference of the Parent Tag object 
protected Tag parent;


//Method to set the value of PageContext
public void setPageContext(PageContext pageContext)
{
this.pageContext=pageContext;
}

//Method to return the value of PageContext
public PageContext getPageContext()
{
return pageContext;
}

//Method to set the value of Parent tag
public void setParent(Tag parent)
{
this.parent=parent;
}

//Method to return the value of Parent tag
public Tag getParent()
{
return parent;
}
//Default implementation of doStartTag()
public int doStartTag()
{
return SKIP_BODY;
}
	
//Default implementation of doEndTag()
public int doEndTag()
{
return EVAL_PAGE;
}
//Other utility methods...
}

From the implementation of TagSupport class, following concepts can be understood:

1. In the user defined Tag class which is a sub class of TagSupport, pageContext and parent data members can be directly used because their scope is protected.

2. If doStartTag() method is not overridden by the user defined Tag class then its body will not be processed because in that case SKIP_BODY will be returned by the inherited doStartTag() method.

3. If doEndTag() method is not overridden by the user defined Tag class then page contents after the tag will always be processed because in that case EVAL_PAGE will be returned by the inherited doEndTag() method.

Defining a custom Tag class:

In order to define a custom Tag class, implantation of Tag interface need to be provided. It can be implemented directly or indirectly with the help of TagSupport class as demonstrated by the following diagram:

tag_implementation

Its time to recap the main concepts:

  1. In order to define a custom Tag class, implementation of Tag interface need to be provided.
  2. TagSupport is a helper class which implements Tag interface on behalf of application programmers.
  3. A custom Tag class can extends TagSupport class to indirectly implement Tag interface.
  4. TagSupport class provides pageContext and parent data members which can be directly used in custom tag class.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

4 thoughts on “TagSupport class of Tag API

  1. Dear Sir,
    Nice and easy to understand content provided..! Thank you for this effort..
    one thing that I have noticed is in TagSupport class mistakenly in place of default implementation of doEnd() tag, it is written doStart() tag….

  2. Respected sir

    if i am right then int doEndtag() is in place of int doStartTag() in your code.

    Default implementation of doEndTag()
    public int doStartTag()
    {
    return EVAL_PAGE;

Leave a Reply

Your email address will not be published. Required fields are marked *