• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tag Handler - how to capture body of a tag???

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I want to capture the body of a tag and then process it and then display it.

Suppose I have following in my code

<my:tahBody>
Display
</my:tahBody>

Now I want to get "Display ABC" and then process it to "yalpsiD" and then it shpuld be displayed...

I am really not sure how to do it. I am really stuck at this chapter...
somebody Please Help.

Thanks in advance,

Sushma
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sushma,

Probably tag should implement an interface which provides body content of the tag.

See if below two links help
http://www.onjava.com/pub/a/onjava/2001/01/18/jsptags.html
http://e-docs.bea.com/wls/docs61/taglib/handler.html#361975

From first one, read Body Tag Interface explanation
If a tag is derived from BodyTagSupport, there are additional methods -- setBodyContent, doInitBody, and doAfterBody -- contained in the BodyTag interface. The additional methods let a tag handler access its body. The body of a tag is anything that comes between the start and end of a tag.

The BodyContent object is a subclass of JspWriter, which is the writer used internally for the JSP out variable. The BodyContent object is available through the bodyContent variable in doInitBody, doAfterBody, and doEndTag. This is important because the BodyContent object contains methods that you can use to write, read, clear, and retrieve content and then incorporate that content into the original JspWriter during the doEndTag.

Cheers
Amr
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amar,

Thanks for the real quick reply and nice link. It did help me, but I am still stuck...
This is what I did, I extended BodyTagSupport in my tag handler class. below is the code for my tag handler.

ClassicBodyTagHandler.java
---------------------------------------------------
package tag;

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

public class ClassicBodyTagHandler extends BodyTagSupport{
JspWriter out;
public int doStartTag() throws JspException{
out = pageContext.getOut();
try{
out.println("<BR> in ClassicBodyTagHandler doStartTag ");
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
return EVAL_BODY_BUFFERED;
}

public void doInitBody() throws JspException{
try{
out.println("<BR>\t the bodyContent initially is " + bodyContent.getString());
StringBuffer bodyBuffer = new StringBuffer(bodyContent.getString());
bodyBuffer.reverse();
bodyContent.print(bodyBuffer.toString());
out.println("<BR>\t the bodyContent is now " + bodyContent.getString());
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
}

public int doAfterBody() throws JspException{
try{
out.println("<BR> In ClassicBodyTagHandler doAfterBody");
}catch(IOException ex){
throw new JspException("IOException = "+ex.toString());
}
return SKIP_BODY;
}

public int doEndTag() throws JspException{
try{
out.println("<BR> In ClassicBodyTagHandler doEndTag");
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
return EVAL_PAGE;
}
}

------------------------------------
I declared the tag in a tld, below is the declaration

<tag>
<description>A Classic BODY Tag </description>
<name>classicBody</name>
<tag-class>tag.ClassicBodyTagHandler</tag-class>
<body-content>scriptless</body-content>
</tag>
----------------------------------------
then, I called this tag from my jsp.
----------------------------------
classicBody.jsp

<%@ taglib prefix="classic" uri="ClassicTags" %>
<html<body>
<Br> <Br> <Br>
<classic:classicBody>I have a Body</classic:classicBody>
<Br> <Br> <Br>
</body></html>

----------------------------------------------
but I get the following result when I run my jsp.


in ClassicBodyTagHandler doStartTag
the bodyContent initially is
the bodyContent is now
In ClassicBodyTagHandler doAfterBody
In ClassicBodyTagHandler doEndTag

I don't know why it is not showing the content of bodyContent?
can you help me please?

Thanks again,

Sushma
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ranchers,

please help,

Sushma
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i think that you have to put the code that is in the doInitBody() into doAfterBody, because when the container get EVAL_BODY_BUFFERED he buffered the content of your tag and then you can manipulate the content, so your doInitBody() keep like this


public void doInitBody() throws JspException{
// yes, nothing
}

public int doAfterBody() throws JspException{
try{
out.println("<BR>\t the bodyContent initially is " + bodyContent.getString());
StringBuffer bodyBuffer = new StringBuffer(bodyContent.getString());
bodyBuffer.reverse();
bodyContent.print(bodyBuffer.toString());
out.println("<BR>\t the bodyContent is now " + bodyContent.getString());
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}

return SKIP_BODY;
}

i hope that i was clear, cause my english i think that is not really good,
regards.
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Irach,
thanks for replying... My code worked after these chages.
I understood, that I should keep my body capturing code in the doAfterBody instead of doInitBody. But, I still don't understand why can't I access bodyContent in doInitBody() method?
if you have any documents or any good links on this topic can you forward them to me??

Thanks again,

Sushma
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was also confused initially but finally I found the solution!

1'st let me explain the process.

1)When doStartTag() returns EVAL_BODY_BUFFERED , setBodyContent() is called.At this point, container passes an instance of BodyContent to the tag handler class.
(Point to be noted here is that when this method completes, we just have a new instance of bodyContent. it still does not have evaluated body string)

2)Then, doInitBody() is called. At this time, still the bodyContent does not contain the evaluated body string(i.e. bodyContent.getString() would return empty String).
This method can be used to initialize or add to the bodyContent( which is again a subclass of JspWriter.)

3)Then the body is evaluated by the container and appended to the bodyContent instance.

4)Then, doAfterBody() is called, which can perform post-evaluation of the bodyContent. This method can return SKIP_BODY or EVAL_BODY_AGAIN . In latter case, the body will be evaluated again, and appended to the bodyContent.

5)Finally doEndTag() is called.

Here is my version:

*******************************************************
package tag;

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

public class ClassicBodyTagHandler extends BodyTagSupport
{
JspWriter out;
int counter;

public int doStartTag() throws JspException
{
//out = pageContext.getOut(); // this will also work!!
counter=0;
return EVAL_BODY_BUFFERED;
}

public void doInitBody() throws JspException
{
try{
bodyContent.print("ADD");
}
catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
}

public int doAfterBody() throws JspException
{
try
{
out=bodyContent.getEnclosingWriter();
StringBuffer text=new StringBuffer(bodyContent.getString());
out.println("<BR>\t the initial bodyContent :"+text);
text.reverse();
out.println("<BR>\t the modified bodyContent :"+text);
counter++;
}
catch(IOException ex)
{
throw new JspException("IOException = "+ex.toString());
}
if(counter==2)
return SKIP_BODY;
else
return EVAL_BODY_AGAIN;
}

public int doEndTag() throws JspException
{
return EVAL_PAGE;
}
}
Output:
the initial bodyContent :ADDI have a Body
the modified bodyContent :ydoB a evah IDDA
the initial bodyContent :ADDI have a BodyI have a Body
the modified bodyContent :ydoB a evah IydoB a evah IDDA


Here you can see that ADD is prefixed to the tag body which I appended in doInitBody().
Also,when counter is 1, body is evaluated again and appended to existing bodyContent instance.

I hope this will answer your question that why we can't access bodyContent in doInitBody(). When doInitBody is called bodyContent is empty i.e body is not yet evaluated.

I have referred SCWCD Study Kit by Manning and the online links given earlier.. but the things were clear only when I tried different experiments.

Regards,
Amit
*****************************************************
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Your explaination really helped me a lot. I tried the simple tags earlier and now I was trying to deal with the body of tags. I am using HFSJ, but there isn't much about modifying the bodyContents, I searched on the internet, but whatever I found wasn't much clear and was generally an overview. These things can certainly be understood after experiments and practice....

Thanks for the help.

Sushma
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually HF book concentrates strictly on certification topics and more rightly so.. its okay to have knowledge but do remember not go into unnecessaary details when ur close to the exam.
The HF book clearly mentions that detailed implementation of BodyTagSupport class is not covered on the exam, though the other books like SCWCD Study Kit by Manning.. don't say so... but we'll have to trust the co-developers say..
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that BodyTagSupport is not in detail in the exam, but I am taking this certification in order to get knowledge.... and I haven't developed any tags earlier, so I wanted to know at least the minimum of it... functionality can be always added...
anyways, thanks for the advice

Regards,

Sushma
 
reply
    Bookmark Topic Watch Topic
  • New Topic