• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam: Head First 1.4 : Doubt

 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Question 9 from Head First 1.4 version of mock exam chapter 10 (Custom tag development)

public class BufTag extends BodyTagSupport {
public int doStartTag() throws JspException {
//insert code here
}
}

Assume, that the tag has been properly configured to allow body content.

Which, if inserted at line 12, would cause the JSP code
<mytags:mytag>BodyContent</mytags:mytag> to output

BodyContent

The answer given by them is : return EVAL_BODY_INCLUDE;

I know that will give the desired output.. But what about return EVAL_BODY_BUFFERED?

It will call the setBodyContent(BodyContent b). then it will cal doInitBody(), and then it will call EVAL_BODY_INCLUDE ( as per the diagram given on page 533). My tiinking is that the diagram is misleading.. I saw the function doInitBody() and there is no code in that method. It means there is no action. So might be the diagram on pg 533 is wrong.

Also I see they have mentioned to refer (JSP v2.0 pg 2-68).. What does this mean? Are they asking to refer JSP Spec 2.0.. What does pg 2-68 means?

Regards

Prem Kashyap
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setBodyContent(BodyConent) only calls when you return EVAL_BODY_BUFFERED
in your doStartTag.

If you want to evaluate tag body via returning EVAL_BODY_BUFFERED you must
override doAfterBody() and that is where you play with tag bodies.

best regards,
omi
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that when we call EVAL_BODY_INCLUDE from doStartTag() then the body is evaluated once, even we do not override the doAfterBody() method.

We override doAfterBody() method when we want to evaluate the body multiple times, and we do that by returning EVAL_BODY_AGAIN from doAfterBody().

So my question is that when we return EVAl_BODY_BUFFERED from doStartTag(), that will execute setBodyContent() and then call doInitBody(). After this where does the control goes? And whether the body gets evaluated once or not.

As per the diagram given in Head First 1.4, pg-533, after doInitBody(), Body will be evaluated.

Regards

Prem Kashyap
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The control goes to doEndTag().

if you return EVAL_PAGE the body will be evaluated which is default return
type of doEngTag() else the body will not be evaluated.

try to return SKIP_PAGE then see what happens.

The actual code is this:


best regards,
omi
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not asking about the return value from doEndTag..
What i am asking, if we return EVAL_BODY_BUFFERED from doStartTag(), then will the body of the tag will be evaluated once? (provided <body-content> is not empty in the TLD)

And giving EVAL_PAGE from doEndTag()will not evaluate the body. It will evaluate the rest of the page after the tag ending.

Regards

Prem Kashyap
 
Vinod Subramaniyam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you return EVAL_BODY_BUFFERED the setBodyContent -> doInitBody -> doAfterBody will be called. The Body contents will be evaluated, stored in Buffer and will not be output.
If EVAL_BODY_INCLUDE is returned then body will be evaluated and written to output.

[ May 13, 2008: Message edited by: Vinod Subramaniyam ]

[ May 13, 2008: Message edited by: Vinod Subramaniyam ]
[ May 13, 2008: Message edited by: Vinod Subramaniyam ]
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinod Subramaniyam:
If you return EVAL_BODY_BUFFERED the setBodyContent -> doInitBody -> doAfter will be called. The Body contents will be evaluated, stored in Buffer and will not be output.
If EVAL_BODY_INCLUDE is called then body will be evaluated and written to output.

[ May 13, 2008: Message edited by: Vinod Subramaniyam ]


Yea although it is confusing ,I was confused too.
[ May 13, 2008: Message edited by: omi sharma ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic