tag life cycle methods

In this post, you will learn:

  • What is tag life cycle?

Tag life cycle:

Tag life cycle describes how and when a tag object is created, initialized, and is used by the tag processor for processing a custom tag. Tag life cycle is defined by javax.servlet.jsp.tagext.Tag interface with the help of following methods:

  1. setPageContext(): It is the first method which is invoked by the tag processor after creating a Tag object. It is used by the Tag Processor to provide the reference of the PageContext object. Role of this object will be explained in the next post. It has following syntax:

public void setPageContext(PageContext context);

  1. setParent(): It is an optional method and is invoked only if the tag is nested. It is used by the Tag processor, to provide the reference of the Tag object of the nesting tag to the current tag. It has following syntax:

public void setParent(Tag parent) ;

  1. doStartTag(): This method is invoked by the Tag processor after the initialization is complete. It represents the beginning of the tag processing. Contents generated by this method replaces the opening tag in the output. It has following syntax:

public int doStartTag();

In addition to generating contents this method also control the processing of the Tag body. It returns an integer which tell the Tag processor whether tag body is to be processed or not. Tag interface provides following static final int data members to be used as its return value: Tag.EVAL_BODY_INCLUDE and Tag.SKIP_BODY.

  1. doEndTag(): This method is invoked by the Tag processor when closing tag of the custom action is encountered. It represents the end of the tag processing. Contents generated by this method replaces the closing tag in the output. It has following syntax:

public int doEndTag();

In addition to generating contents this method also control the processing of the rest of the page. It returns an integer which tell the Tag processor whether rest of the page is to be processed or not. Tag interface provides following static final int data members to be used as its return value: Tag.EVAL_PAGE and Tag.SKIP_PAGE.

Following diagram describes the flow of life cycle methods.

tag_life_cycle

Notes:

1. setParent() – method is invoked only if the tag is nested.

2. setter – methods of attributes are invoked only if the tag has attributes.


Its time to recap the main concepts:

  1. Tag interface defines life cycle methods of a custom tag.
  2. These methods are invoked by Tag Processor and defined by application programmers.
  3. Tag processing has two phases: initialization and processing.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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