File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock Exam Question - related to doAfterBody Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock Exam Question - related to doAfterBody" Watch "Mock Exam Question - related to doAfterBody" New topic
Author

Mock Exam Question - related to doAfterBody

Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
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


SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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

Joined: Jun 13, 2005
Posts: 95
I saw the thread on this forum:
http://www.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

Joined: Jun 02, 2005
Posts: 139
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
 
 
subject: Mock Exam Question - related to doAfterBody
 
Similar Threads
Can anyone explain this to me?
Edurardo Cobian's mock exam answer correct for Question No 25
Custom Tag Handler
Exam question.
what happens when doStartTag returns EVAL_BODY_BUFFERED?