Custom Tag BodyTagSupport

Custom Tag BodyTagSupport


Posted in : Java Posted on : February 8, 2012 at 7:10 PM Comments : [ 0 ]

In this tutorial you will learn about BodyTagSupport class and how it can be used.

Custom Tag BodyTagSupport

In this tutorial you will learn about BodyTagSupport class and how it can be used.

BodyTagSupport is a class of package javax.servlet.jsp.tagext that extends the class TagSupport and implements the BodyTag interface. Since this class implements the BodyTag interface so all the methods for supporting a tag handler class is implemented already besides these methods this class provides to add an additional functionality to set the property of bodyContent. This class is used as a base class to pass attributes and bodyContent however attribute passing is not essential. Use of this class is not useful if you do not want to manipulate the bodyContent.

Example :

Here I am giving an example which will demonstrate you how to manipulate the body content using BodyTagSupport class. To achieve the solution of such problem at first I have created a tag handler class named TagHandlerBodyTag.java that extends a class BodyTagSupport. Then I have created a TLD file named bodyTagSupport.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page named customTagBodyTagSupport.jsp.

TagHandlerBodyTag.java

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

public class TagHandlerBodyTag extends BodyTagSupport
{
protected BodyContent bodyContent;

public int doStartTag()
{
return EVAL_BODY_BUFFERED; 
}
public int doEndTag()
{
try
{
bodyContent = getBodyContent();
String str = bodyContent.getString();
JspWriter out = bodyContent.getEnclosingWriter();
if(str != null)
{
out.println("<br>"+str.toLowerCase()); 
}
} catch(Exception e)
{
System.out.println(e);
}
return EVAL_PAGE;
} 
}

bodyTagSupport.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>ascii</name>
<tag-class>pack.TagHandlerBodyTag</tag-class>
<bodycontent>JSP</bodycontent>
</tag>
</taglib>

customTagBodyTagSupport.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/bodyTagSupport.tld" prefix="dev" %>
<html>
<head>
<title>BodyTagSupport Example</title>
</head>
<body>
<b>content of JSP page body....</b>

<form action="">
Enter characters in capital letter <input type="text" name="text"/>

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

Output :

When you will execute the jsp page the following output will be displayed as :

1.

 

2. When you will enter the value into text box as

3. After enter the value when you will click on submit button then the output will be as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics