The IterationTag Interface in JSP
By: Henry in JSP Tutorials on 2007-10-06
The IterationTag interface was introduced as a part of the JSP 1.2 specification in an attempt to simplify the procedure for evaluating and reevaluating body content multiple times. This was previously possible with the JSP 1.1 BodyTag interface, although the implementation details were complex.
IterationTag.java
package javax.servlet.jsp.tagext;
import javax.servlet.jsp.JspException;
public interface IterationTag extends Tag {
public final static int EVAL_BODY_AGAIN = 2;
int doAfterBody() throws JspException;
}
The interface itself is fairly small, and this is because it extends the Tag interface. Effectively, a single method has been added to the tag life cycle called doAfterBody(), and it's this method that provides you with the ability to ask that a tag's body content is evaluated more than once. Let's see how this affects the tag life cycle.
The Iteration Tag Life Cycle
Figure below illustrates the iteration tag life cycle and shows where the new doAfterBody() method fits in with the life cycle of a custom tag.
To support iteration, the doAfterBody() method is called after doStartTag(). The return value of doAfterBody() indicates whether the tag body is evaluated again or processing continues to the doEndTag() method.
Setting the Context
The first thing that happens is that the contextual information (the PageContext and parent Tag) is passed to the tag handler instance.
The Start Tag
After the context has been set, the doStartTag() method is called and one of two things can happen. First, the doStartTag() method can return SKIP_BODY, meaning that the body content is ignored and processing proceeds to the doEndTag() method as before. Alternatively, a value of EVAL_BODY_INCLUDE can be returned from the doStartTag() method, signaling to the JSP container that the body content should be evaluated and included in the page. Again, this is the same as with the Tag interface, although it's after the body content has been evaluated that things start to change.
After the Body
After the body content has been evaluated, the new doAfterBody() method is called, the primary purpose of which is to determine whether the body content should be reevaluated. If this is the case, the method should return the EVAL_BODY_AGAIN constant defined within the IterationTag interface. On the other hand, and when no more evaluations of the body content should happen, the SKIP_BODY value should be returned.
The End Tag
Finally, regardless of whether or not the body was evaluated and reevaluated multiple times, the doEndTag() method is called in the same way as the other tags that you've seen in this chapter. Again, possible return values are EVAL_PAGE and SKIP_PAGE.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments