Home Jsp BodyTagSupport Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

BodyTagSupport Example

Advertisement
BodyTagSupport class implements the BodyTag interface and you can add additional functionalities in it to set property of bodyContent.

BodyTagSupport Example

     

Example to illustrate the use of BodyTagSupport in a JSP page

BodyTagSupport class implements the BodyTag interface and you can add additional functionalities in it to set property of bodyContent. If you want to add other functionalities in this tag then there are some methods which you have to override. Some important methods of  this class are as follows:

  • doAfterBody() -After the body evaluation it  does not reevaluate and continue with the page
  • doStartTag() - default working of start tag returns EVAL_BODY_BUFFERED
  • doEndTag() - default working of end tag returns EVAL_PAGE
  • getBodyContent() - it gets current bodyContent

This example will illustrate you how to make custom tags in JSP along with implementation of BodyTagSupport's methods. To make custom tags in JSP and to use them, we have made an application which have <tbl:stag> </tbl:stag> tag which is being used to convert all contents of body of the tag to Upper case. For example, if it is written like this in JSP page

<tbl:stag>
hello ! i am Amit Kumar Raghuwanshi
</tbl:stag>

So when JSP page would be executed then this tag will convert the body content "hello ! i am Amit Kumar Raghuwanshi" to the following source code in HTML like this:

<body>
HELLO ! I AM AMIT KUMAR RAGHUWANSHI
</body>

To implement these functionalities we have created following files:

  • MyTag.java
  • BodyTagExample.jsp
  • stag.tld
MyTag.java is a simple java file which is extending BodyTagSupport class. In this file we have overridden only one method of BodyTagSupport, doAfterBody(), which is firstly taking  the bodyContent to a string named "body" and then bodycontent.getEnclosingWriter() is returning object of JspWriter type and these contents are to be converted in UpperCase and then it will be displayed on the browser. This method doAfterBody() is returning SKIP_BODY means that after the body evaluation :do not reevaluate and continue with the page. Full code of this MyTag.java is given as below :

1. MyTag.java

package stag;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

public class MyTag extends BodyTagSupport 
{
  public int doAfterBody() throws JspException {
  try {
 BodyContent bodycontent = getBodyContent();
 String body = bodycontent.getString();
 JspWriter out = bodycontent.getEnclosingWriter();
 if(body != null) {
  out.print(body.toUpperCase());
 }
  catch(IOException ioe) {
 throw new JspException("Error:"+ioe.getMessage());
  }
  return SKIP_BODY;
  }

}

Another very important file is stag.tld which is playing very important role in this example. This "stag.tld" is an XML file which is setting the shortname, name, bodycontent ,tagclass of this tag. Using these parameters setting in this tld file we are going to make use of it in our JSP page in the following manner:

<%@ taglib uri="/WEB-INF/stag.tld" prefix="tbl" %>

Code for stag.tld  is as following :

2. stag.tld

<?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/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>

  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>tbl</shortname>
   <tag>
  <name>stag</name>
  <tagclass>stag.MyTag</tagclass>
  <bodycontent>JSP</bodycontent>
  <info>To Convert in Upper case tag</info>
   </tag>
</taglib>

JSP page "BodyTagExample.jsp" full code is here:

3. BodyTagExample.jsp

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/stag.tld" prefix="tbl" %>

<html>
<body>
<tbl:stag>
hello ! i am Amit Kumar Raghuwanshi
</tbl:stag>

</body>
</html>

To run this example you have to follow these few steps :

  • Create file MyTag.java and save it.
  • Compile and put this MyTag.class file into WEB-INF/classes folder.
  • Create and save stag.tld into WEB-INF folder.
  • Create and save BodyTagExample.jsp file.
  • Start and run tomcat server and type the following URL into address bar of your browser
    http://localhost:8080/vin/BodyTagExample.jsp

Output:

This is showing all body content in Upper Case letter as we have done in our tag functionality. The HTML source code generated would be like this:

Any content which is not in <tbl:stag> will remain as it is. For example 

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/stag.tld" prefix="tbl" %>
<html>
<body>
<tbl:stag>
hello ! i am Amit Kumar Raghuwanshi
</tbl:stag>
<br>
This will remain as it is ...
</body>
</html>

and its output would be like this:

Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 5, 2008

Ask Questions?    Discuss: BodyTagSupport Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
ashok
April 24, 2012
BodyTagSupportexample

Apr 24, 2012 1:19:05 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet jsp threw exception java.lang.OutOfMemoryError: Java heap space
DMCA.com