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