Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Edurardo Cobian's mock exam answer correct for Question No 25

 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question No 24, 25, 26 are good examples for how the output will looks like for custom Tag implemention.
For question no 25
Given the following tag handler defined with <bodycontent>JSP</bodycontent>
public class body extends TagSupport {
public int doStartTag() throws JspException{
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
try { pageContext.getOut().print("how are you?"); }catch(IOException e) {}
return SKIP_BODY;
}
}
what will be printed out by the following part of a jsp page?
<prefix:sufix>
<i>Hello</i>
</prefix:sufix>
1) The tag handler won't compile.
2) The jsp page will print Hello how are you?
3) The jsp page will print how are you? Hello
4) The jsp page will print Hello
I am quite sure the correct is Hello how are you?
No 2. not No 4 in the answer sheet.
You can sure print using pageContext.getOut() from doAfterBdoy in a TagSupport.
Note that TagSupport doesn't provide bodyContent object because it is defined in BodyTag interface which is implemented in BodyTagSupport
 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question No 26 �
Given the following tag handler defined with <bodycontent>JSP</bodycontent>
public class body extends BodyTagSupport {
public int doAfterBody() throws JspException {
try { pageContext.getOut().print("how are you?"); }catch(IOException e) {}
return SKIP_BODY;
}
}
what will be printed out by the following part of a jsp page?
<prefix:sufix>
<i>Hello</i>
</prefix:sufix>
1) The tag handler won't compile.
2) The jsp page will print Hello how are you?
3) The jsp page will print how are you? Hello
4) The jsp page will print Hello
There is no listed correct answer here.
The jsp page will print nothing.
"Hello" is the body content here, if you want to print it, you will need to use getBodyContent().writeOut(getPreviousOut()); to write the content.
The pageContext.getOut() here is still the JSPWriter object for the tag. But because
it is buffered in BodyTagSupport by default, it will print to it's own JSPWriter, but not the out JSPWriter of the page. You cannot see anything print out here.
If we overwrite the doStartTag() here and return as EVAL_BODY_INCLUDE. It will not call setBodyContent adn doInitBody() then. The process will just like the TagSupport(), all print are to the JSPWriter of the page directly.
Here will print
Hello, How are you?
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jason;

Impressed.
I agree with u.
keep it up.
just for fun,try this.
<body-content>JSP</body-content>
public class body extends BodyTagSupport {
int count=3;
public int doAfterBody() throws JspException {
if(count-- >= 1){
BodyContent b=getBodyContent();
try{
JspWriter out=b.getEnclosingWriter();
out.println(b.getString());
}catch(Exception e){}
return (EVAL_BODY_AGAIN);
}else{
return(SKIP_BODY);
}}}
now try adding this b.clearBody();after
out.println(b.getString());
<prefix:test>
just for fun!
</prefix:test>

"no worries"
"Happiness is a cigar called mild"
Amer
[ May 22, 2003: Message edited by: Amer Khan ]
 
Chan sekhar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 25>>
Answer is 4
The doAfterBody() will not be executed because the body class extends TagSupport, not BodyTagSupport.
The doStartTag() method returns EVAL_BODY_INCLUDE, ensuring that the tag body will be printed. The result is that Hello will be printed.
 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 25>>
Answer is 4
The doAfterBody() will not be executed because the body class extends TagSupport, not BodyTagSupport.
The doStartTag() method returns EVAL_BODY_INCLUDE, ensuring that the tag body will be printed. The result is that Hello will be printed.

doAfterBody() is defined in IterationTag and implemented by BodyTag.
doInitBody is defined in BodyTag and implemeneted by BodyTagSupport.
So it works.
[ May 22, 2003: Message edited by: Jason Qiu ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic