doAfterBody() IterationTag Example

doAfterBody() IterationTag Example


Posted in : Java Posted on : March 28, 2012 at 7:37 PM Comments : [ 0 ]

In this tutorial you will learn how to use doAfterBody() in jsp to handle custom tags.

doAfterBody() IterationTag Example

In this tutorial you will learn how to use doAfterBody() in jsp to handle custom tags.

doAfterBody() method of IterationTag interface is used to control the reevaluation of tag body. Whether the body tag will be reevaluated or not is depend on the value it returns (discussed later). If there is no body evaluation this method will not be called by the jsp page implementation object. This function returns static final integer constant value on which it is depend that the body has to be reevaluated or not is as follows :

  • EVAL_BODY_AGAIN : This constant field is defined in IterationTag interface. If doAfterBody() method returns this value it means it request for evaluation of the body again.
  • SKIP_BODY : This constant field is defined in Tag interface and the IterationTag interface extends the Tag interface so this value can be inherited from the Tag interface and can be used easily. So when doAfterBody() method returns this value it means the body will be skipped i.e the body of tag will not be execute.

Example :

Here I am giving a simple example which will demonstrate you how to use the doAfterBody() tag handler method and the constant integer values. To do so at first I have created a tag handler class named DoAfterBodyExample.java that extends the BodyTagSupport class ( I have used here the BodyTagSupport class to define a tag handler class because it is easy to use, you can use the IterationTag interface but if you will use this interface you will be required to implement all the methods defined in it )

DoAfterBodyExample.java

package pack;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class DoAfterBodyExample extends BodyTagSupport
{ 
int count;


public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public int doStartTag()
{
count = 1;
return EVAL_BODY_INCLUDE;
}

public int doAfterBody() throws JspException
{
while (count < 3)
{ 
count++;
return EVAL_BODY_AGAIN;
}
return SKIP_BODY; 
}
}

doAfterBodyExample.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>ascii</name>
<!-- <tag-class>pack.TagHandlerBodyTag</tag-class> -->
<tag-class>pack.DoAfterBodyExample</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>count</name>
<required>true</required>
</attribute>
</tag>
</taglib>

doAfterBodyExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/doAfterBodyExample.tld" prefix="dev" %>
<html>
<head>
<title>doAfterBody() Example</title>
</head>
<body>
<b>content of JSP page body....</b>

<form action="">
Enter Text <input type="text" name="text"/>

<%
String str= request.getParameter("text");
%>
<input type="submit" value="submit"/>
</form>
<b>contents of custom tag body....</b><br>
<dev:ascii count="3">
<%=str %>
</dev:ascii>
</body>
</html>

Output :

When you will deploy and execute your jsp file then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics