JSP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Iterator

JSP Iterator

shape Description

The JSP Iterator utilizing the doAfterBody() strategy by repeating the body substance of any tag. TagSupport tag which executes the IteratorTag interface, can be utilized for emphasizing the body content. One have to utilize EVAL_BODY_AGAIN consistent in the doAfterBody().

Iterator

shape Example

JSP Iterator - Before the example one should have to learn tag lib, The JSP Taglib was introduced in JSP 1.1. These libraries allow the addition of tags similar to jsp:forward, jsp: include with different prefixes. The JSP Taglib directives are used to define tag library with a prefix that can use current JSP page. JSP Taglib might include several tag libraries. Following are the steps to be performed while working with custom tags.  iterator.jsp [java] <%@ taglib uri="WEB-INF/mytags1.tld" prefix="m" %> 3 ^ 5 = <m:power number="3" power="5"> DO </m:power> [/java] JSP Iterator - In the URL just placed a link, prefix attribute informs a container what bits of markup are custom actions. mytags1.tld [java]<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>simple</short-name> <uri>http://tomcat.apache.org/example-taglib</uri> <description>A simple tab library for the examples</description> <tag> <name>power</name> <tag-class>PowerNumber.PowerNumber</tag-class> <attribute> <name>number</name> <required>true</required> </attribute> <attribute> <name>power</name> <required>true</required> </attribute> </tag> </taglib> [/java] Here Java class name has been declared with it's package name. PowerNumber.java [java] package PowerNumber; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class PowerNumber extends TagSupport{ private int number; private int power; private static int counter; private static int result=1; public void setPower(int power) { this.power = power; } public void setNumber(int number) { this.number = number; } public int doStartTag() throws JspException { return EVAL_BODY_INCLUDE; } public int doAfterBody() { counter++; result *= number; if (counter==power) return SKIP_BODY; else return EVAL_BODY_AGAIN; } public int doEndTag() throws JspException { JspWriter out=pageContext.getOut(); try{ out.print(result); }catch(Exception e){e.printStackTrace();} return EVAL_PAGE; } } [/java] Tag handler method doStartTag() is defined by a Tag interface that is called by the Jsp page’s servlet when a start tag is evaluated. doStartTag() method is invoked when a start tag of custom tag is found. This function returns static final integer constant value that are already defined in the interface such as follows. SKIP_BODY is an optional returned value but this value must be returned by doStartTag() when you want to skip the body evaluation that is it must be returned when the TagLibraryDescriptor file contains the element empty, the value “empty” shows that there will always be an empty action.doAfterBody() is used to control the reappraisal of tag body. [java]public int doAfterBody() { counter++; result *= number; if (counter==power) return SKIP_BODY; else return EVAL_BODY_AGAIN; } [/java] Output Now compile the code result will be as follows.

Summary

shape Key Points

  • JSP Iterator - TLD should be in WEB-INF.
  • Created JSP file should be in web.xml .
  • JAVA file should be under JSP java resources folder.