The BodyTag Interface in JSP

By: Ivan Lim Emailed: 1646 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The classic tags can evaluate their body content zero, or one or more, times. This is particularly useful when the content to be evaluated is trivial or, in other words, when no transformation or manipulation of the content is required before it’s output to the page. If such transformation is required prior to the content being written to the page, you must turn to the BodyTag interface.

The BodyTag Interface

The BodyTag interface further extends the IterationTag interface to add even more flexibility and capability.

BodyTag.java

package javax.servlet.jsp.tagext;
import javax.servlet.jsp.JspException;
public interface BodyTag extends IterationTag {
public final static int EVAL_BODY_BUFFERED = 2;
void setBodyContent(BodyContent b);
void doInitBody() throws JspException;
}

Once again, this interface adds another new constant and two methods that are related to the body content of the tag in question. As you might expect, this means a slightly different life cycle for body tags.

The Body Tag Life Cycle

Figure below summarizes the tag life cycle.

The life cycle of a tag handler that implements the BodyTag interface

Setting the Context

As with all of the other classic tags, first the contextual information (the PageContext and parent
Tag) is passed to the tag handler instance.

The Start Tag

After the context has been set, the doStartTag() method is called, and with the BodyTag interface there are three different return values. The doStartTag() method can return SKIP_BODY, meaning that the body content is ignored and processing proceeds to the doEndTag() method, or it can return EVAL_BODY_INCLUDE, signaling that the body content should be evaluated and included in the page. The difference is that the doStartTag() method can now return the EVAL_BODY_BUFFERED constant defined in the BodyTag interface. Returning this value indicates to the JSP container that you want to use the features provided by the BodyTag interface—specifically, that the tag handler will manipulate the body content.

Setting the Body Content

Assuming that you return EVAL_BODY_BUFFERED from the doStartTag() method to indicate that you want to manipulate the body content, the setBodyContent() method is called on the tag handler so that the tag can hold on to the BodyContent reference and use it later. Simple tags have a similar method called setJspBody() that passes a JspFragment instance representing the actual body of the tag. With the BodyTag interface, this process is slightly different. Instead of being passed a JspFragment representing the body content that can subsequently be invoked, the tag handler is passed an object of type BodyContent.

Generating content was simply a matter of finding the JspWriter instance associated with the page and outputting the content. The BodyContent class is a subclass of JspWriter and can be thought of as a temporary scratch-pad area to which content can be written. Behind the scenes, when the JSP container calls the setBodyContent() method, the regular output stream (the JspWriter) is swapped out for a BodyContent object—the same one that gets passed to the tag. This means that any content output from this point onward (until the end tag is reached) is actually written into this temporary scratch pad and not to the page.

The JSP container then calls the doInitBody() method, which can be used to set up any state before the body content is eventually evaluated. The effect of replacing the original JspWriter is that when evaluated, any content between the start and end tags is also written into the BodyContent object, providing you with a way to access the generated content and manipulate it later.

After the Body

As with the IterationTag interface, the doAfterBody() method is called after the body content has been evaluated. There are no changes here; the method should return EVAL_BODY_AGAIN or SKIP_BODY to signal whether more evaluations of the body content are required.

The End Tag

Finally, regardless of whether or not the body was evaluated and reevaluated multiple times, the doEndTag() method is called. Again, possible return values are EVAL_PAGE and SKIP_PAGE. At this point in the life cycle, all of the body content has been evaluated and output into the BodyContent object (the temporary scratch pad). With this in mind, you can now take this content and manipulate/transform it. When you’re done, you can then write the final result to the original JspWriter instance.


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Hi,
Nice example....it's a good one to implement

Thank you,
Anish


View Tutorial          By: Anish at 2009-07-08 00:21:16
2. View Comment

example required

View Tutorial          By: revathy at 2009-09-03 01:53:04

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ivan Lim
Requesting a Signed Certificate from a Certification Authority example using keytool in Java
The if-then Statement in Java
SELECT Statements
The BodyTag Interface in JSP
Handling Duplicate Form Submissions in Struts
Standard Input and Output in C
A sample that shows Java Beans, Servlets and JSP working together
Recursion in C
Using Multibox in Struts
switch in C
Word Counting sample program in C
Java Bean Scopes in JSF
Using cout.width() in C++
Types of configurations in J2ME
How to get the CLDC and MIDP version from a J2ME program

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP CheckBox Example
JSP Program for display Date
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
What are the different scopes in JSP?
JSP Alert Example
Comparison operators in JSP
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
Automatically Refreshing a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
Protecting your website with a login page
Form Object
Most Emailed Articles (in JSP)
Steps to get a Free SSL certificate for your Tomcat
What is JSP?
Retrieving a Portion of a String
Tags using in jsp
Tag libraries
Closing Windows
Radio Object
Frame Object
Math Object
Disabling Scriptlets in JSP using web.xml
Getting HTTP Request Headers in a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
What is JSP?
Click to Activate and Use this control
Built-in Object in Javascript