• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pushBody() and popBody

 
howard han
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is from Eduardo Cobian's mock exam.
Which of the following statements PageConext methods pushBody and popBody are true?
1) pushBody is called by the jsp container after doStartTag and before doAfterBdy
2) pushBody is called by the jsp container before doInitBody
3) popBody is called by the jsp container after doAfterBody and before doEndTag
4) popBody is called by the jsp container after doEndT
Correct answer is 1) and 3).
The following is from Natalie Levi's Study guide.
"When the container accesses the opening tag, it calls the action´┐Żs doStartTag() method, which causes the pageContext to call its pushBody() method. This method creates a new BodyContent object for that particular tag"
From that 1) is wrong and 2) should be correct.
I can't find in API specification talking about when popBody and pushBody is called.
Can anyone help to shed some light on it? Thanks a lot!
 
howard han
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just write the following code and jsp trying to test it.
/* testtag.java */
package tag;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class testtag extends BodyTagSupport {
public void doInitBody() throws JspException {
try {
BodyContent bc = getBodyContent();
JspWriter out = bc.getEnclosingWriter();
bc.writeOut(out);
out.println("In doInitBody");
}
catch(IOException ie)
{
throw new JspException("init");
}
}
public int doAfterBody() throws JspException {
try {
BodyContent bc = getBodyContent();
JspWriter out = bc.getEnclosingWriter();
bc.writeOut(out);
out.println("In doAfterBody");
}
catch(IOException ie)
{
throw new JspException("init");
}
}
public int doEndTag() throws JspException {
try {
BodyContent bc = getBodyContent();
JspWriter out = bc.getEnclosingWriter();
bc.writeOut(out);
out.println("In doEndTag");
}
catch(IOException ie)
{
throw new JspException("init");
}

}
}
<!-- testtag.jsp -->
<%@ taglib uri="http://lhan" prefix="pre" %>
<pre:testtag>
HanLei
</pre:testtag>
The output is as follows
In doInitBody HanLei In doAfterBody HanLei In doEndTag
From the output it looks like BodyContent existing in doInitBody(), doAfterBody() and doEndTag(). So it looks to me like
pushBody() is called before doInitBody() and
popBody() is called after doEndTag().
So the correct answer should be 2) and 4) instead.
Please help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic