IterationTag Interface Test Class -

This section provides a tutorial example on how to write a Java tag class that implements the javax.servlet.jsp.tagext.IterationTag interface.

In order to confirm my understanding of the IterationTag interface, I wrote the following tag class to print a short message from each implemented method to show when it is called. I also used a very simple logic in doAfterBody() to force the tag body being evaluated twice.

 * Copyright (c) 2002 All Rights Reserved.
package herong;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class TraceTag implements IterationTag {
   private boolean stop = false;
   private PageContext pc = null;
   private Tag t = null;
   public void setPageContext(PageContext pc) {
      this.pc = pc;
      println("setPageContext() called.");
   public void setParent(Tag t) {
      this.t = t;
      println("setParent() called.");
   public Tag getParent() {
      println("setParent() called.");
      return t;
   public void setMyAtt(String v) {
      println("setMyAtt() called.");
   public int doStartTag() {
      println("doStartTag() called.");
      return EVAL_BODY_INCLUDE;
   public int doAfterBody() {
      println("doAfterBody() called.");
      if (!stop) {
         stop = true;
         return EVAL_BODY_AGAIN;
      } else {
         return SKIP_BODY;
   public int doEndTag() {
      println("doEndTag() called.");
      return EVAL_PAGE;
   public void release() {
      println("release() called.");
   private void println(String s) {
      try {
      } catch (IOException e) {

In this tag class, I was planning to have one attribute for my trace tag. Attributes of a tag need to be implemented as properties in the tag class. The setMyAtt(String v) method define a property called "myAtt" for my trace tag.

After compiling my trace tag class, I copied TraceTag.class to \local\tomcat\webapps\ROOT\WEB-INF\classes\herong directory. I had to put it under subdirectory "herong", because the tag class was defined in "herong" package.

Then I updated my tld file, HyTaglib.tld, with a new tag element:

<?xml version="1.0"?>
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
<!-- HyTaglib.tld
 - Copyright (c) 2006 All Rights Reserved.
<short-name>Herong's Tag Library</short-name>
<!-- other tags -->

Since my trace tag will have body, I set body-content to "jsp" instead of "empty". The updated TLD file was copied to \local\tomcat\webapps\ROOT\WEB-INF\tlds directory.

Restart the Tomcat server. Now my "hy:trace" tag is ready to use in JSP pages.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

JSP Java Tag Interface

 javax.servlet.jsp.tagext.* Package

 javax.servlet.jsp.tagext.IterationTag Interface

IterationTag Interface Test Class -

 IterationTag Interface Test JSP - TraceTagTest.jspx

 Servlet Class Converted from JSP -

 TagSupport Class - Dummy Implementation of IterationTag

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials


 Full Version in PDF/EPUB