• 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

Mock Exam Question - related to doAfterBody

 
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,

Following is the Practice exam question :

Q. Given the following tag handler defined with the <bodycontent>JSP</bodycontent>
-------------------------------------------------------
<code>
public class ClassicDoAfterBodyHandler extends BodyTagSupport{
public int doAfterBody() throws JspException{
try{
pageContext.getOut().print("How Are you?");
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
return SKIP_BODY;
}
}

</code>
-----------------------------------------------------
what will be printed out by the following part of a jsp

<prefix:suffix>Hello</prefix:suffix>

Choose one correct answer:

A. The Tag Handler won't compile.
B. The jsp page will print Hello How are you?
C. The jsp page will print How are you? Hello
D. The jsp page will print Hello

The correct answer given is D, But as per my understanding none of the answers is correct and nothing will be printed.
The explanation given is :

Inside doAfterBdoy the bodyContent is in a stack and the implicit variable out prints into the bodyContent not to the outputStream.The bodyContent keeps a reference to the buffered writer (either a enclosing bodyContent or the implicit out variable) called the previousOut.If you want to print inside doAFterBdoy you have to use this previousOut. To print things unrelated with the body use pageContext.getOut in doStartTag. By default doStartTag in BodyTagSupport returns EVAl_BODY_BUFFERED which produces a stack of bodycontents, one bodycontent for each iteration.If you don't want this stack to be create you have to override doStartTag and return EVAL_BODY_INCLUDE, then the default out would print to the output stream (to the response).

Can anybody help me understand why Hello will be printed?

Regards,

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
Hi Sushma!

First of all I would like to thank you coz your example helped me to clear one of my earlier doubt. I'd read earlier that in case of BodyTagSupport, the JspWriter buffers content and does not directly write to JSP page. But when I tried to implement this, I found that the JspWriter instance was writing to the JSP instead of appending to bodyContent.

Now I'll come to your problem...

The following was ur code:
public class ClassicDoAfterBodyHandler extends BodyTagSupport{
public int doAfterBody() throws JspException{
try{
pageContext.getOut().print("How Are you?");
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
return SKIP_BODY;
}
}

I tried the above code but it did not print anything.

Then I tried the following code:
***************************************************
package tag;

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

public class ClassicBodyTagHandler extends BodyTagSupport{

JspWriter out1;
JspWriter out2;
JspWriter out3;

public int doStartTag() throws JspException
{
out1 = pageContext.getOut();
return EVAL_BODY_BUFFERED;
}

public void doInitBody() throws JspException{

}

public int doAfterBody() throws JspException{
try{
out1.print("From printwriter reference in doStartTag<BR>");
out2=pageContext.getOut();
out2.print("How Are you?");
out3=bodyContent.getEnclosingWriter();
out3.print(bodyContent.getString());
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
return SKIP_BODY;
}

public int doEndTag() throws JspException{
return EVAL_PAGE;
}
}
********************************************
The output is:
From printwriter reference in doStartTag
HelloHow Are you?
*********************************************

Hence I conclude:
1) If we override doStartTag and obtain a ref to JspWriter in doStartTag,
it will print to JSP in doAfterBody.

2)If we obtain another ref to JspWriter in doAfterTag, it will buffer the contents..ie. will append to bodyContent.

3)Now, to print the body to JSP, we'll have to obtain enclosed JspWriter of bodyContent using getEnclosingWriter or use the JspWriter obtained in doStartTag or else nothing will be printed

4)As far as "Hello" is concerned, its appended to bodyContent when the body is evaluated by container and before call to doAfterBody.

I hope now your doubt will be clear

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
it means the answer given is wrong and my understanding was correct...

Thanks Amit for such a nice explanation.

Regards,

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
I saw the thread on this forum:
https://coderanch.com/t/169733/java-Web-Component-SCWCD/certification/HFSJ-page

According to it, if we override doStartTag() and return EVAL_BODY_INCLUDE,Then I think "Hello How are you?" will be printed.
What do you say?
(I've not yet tested this)

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
In my opinion, it should be "Hello How are you", so I agree with you on this.
Since doStartTag() is returning EVAL_BODY_INCLUDE, no new body content will be created and the bodyContent will be evaluated resulting in printing of "Hello". Then doAfterBody() will be executed and "How are you?" will be printed as the default "out"(pageContext.getOut()) would print to the output stream (to the response).

Regards,

Sushma
 
reply
    Bookmark Topic Watch Topic
  • New Topic