Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
BodyTagSupport Example 
 

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

                         

» View all related tutorials
Related Tags: java c string jsp ide class strings script object io objects method get char ip using sequence id declaration length

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.