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 doAfterBody() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doAfterBody()" Watch "doAfterBody()" New topic
Author

doAfterBody()

Kanishka Ajwani
Ranch Hand

Joined: Nov 30, 2009
Posts: 62
In which of the following cases will the method doAfterBody() of a tag handler be invoked?


(a) This method will always be called when the body of the tag is not empty.
(b) This method will be called only when the body of the tag IS empty.
(c) This method will be called only when doStartTag() returns Tag.EVAL_BODY_INCLUDE or BodyTag.EVAL_BODY_BUFFERED.
(d) This method will be called only when doEndTag() returns Tag.EVAL_BODY_INCLUDE or BodyTag.EVAL_BODY_BUFFERED.
(e) This method will be called if doAfterBody() returns IterationTag.EVAL_BODY_AGAIN.


According to the author, the correct answer is only "e". However, I think it should be "c and e", since doAfterBody() will be called only if doStartTag() does not return SKIP_BODY (which means it either returns EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED).

Can anyone please explain this to me. Thanks.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

QuoteYourSources Please!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Kanishka Ajwani
Ranch Hand

Joined: Nov 30, 2009
Posts: 62
This is a question from topic discussions on www.enthuware.com. I do not know if I am allowed to share the link. If yes, I would love to.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Kanishka Ajwani wrote:I do not know if I am allowed to share the link. If yes, I would love to.

Questions coming from Enthuware are allowed in this forum.


[My Blog]
All roads lead to JavaRanch
Kanishka Ajwani
Ranch Hand

Joined: Nov 30, 2009
Posts: 62
This question is from the link :
http://www.enthuware.com/forum/viewtopic.php?f=31&t=13

Check out question no 22.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

Hi Kanishka,

I think it should be "c and e", since doAfterBody() will be called only if doStartTag() does not return SKIP_BODY (which means it either returns EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED).

These questions are always a bit tricky. The thing here is that you should know in which interface the static fields (like BodyTag.EVAL_BODY_BUFFERED) are declared.

EVAL_BODY_INCLUDE is declared in the IterationTag interface and not in the Tag interface. Conclusion: Tag.EVAL_BODY_INCLUDE doesnot exist and therefore answer "c" is incorrect

Regards,
Frits
Kanishka Ajwani
Ranch Hand

Joined: Nov 30, 2009
Posts: 62
But the question does not tell you which interface/class is the tag handler implementing/extending. When you call a class "tag handler", does it mean that it is implementing the Tag interface?

I could be wrong here.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

But the question does not tell you which interface/class is the tag handler implementing/extending. When you call a class "tag handler", does it mean that it is implementing the Tag interface?

Not always, you have to look at the methods they are giving you in the question. In this question they are only mentioning doStartTag() and no doTag() so you question is only about the "Classic" Custom Tags. (which all implement the Tag interface though). The Simple Custom Tag, doesnot implement the Tag interface.

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy guys!

I've got a similar problem with the Classic Tags and doAfterBody() method.

I mean, according to this question (answer e.) as well as the K&B Head First book (page 595, question 10),

Assuming no exceptions occur, doAfterBody() is called after doStartTag() for any tag that implements IterationTag and returns EVAL_BODY_INCLUDE from doStartTag().


My question is - is this enough for the doAfterBody() to execute? Doesn't the tag itself has to be used with the body?
I mean, I can have:
- a body-content set to "scriptless" in the TLD,
- Tag handler can extend IterationTagSupport,
- implemented the doAfterBody() method,
- doStartTag() method can return EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED,

but the doAfterBody() won't be executed if I have something like:

<pedro:myClassicTag />


Am I missing something, or the question here and in the K&B Head First book is slighly imprecise?

Thanks in advance!

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

Hi Pedro,

Maybe a bit late with replying, but I am just catching up after a two weeks holiday

My question is - is this enough for the doAfterBody() to execute? Doesn't the tag itself has to be used with the body?

Yes it is enough, have a look at my notes (ScwcdLinks, rancher notes ch 8.5). According to the specs, the body of the tag is evaluated after the doStartTag() method returns a EVAL_BODY_INCLUDE.

a body-content set to "scriptless" in the TLD

the returncode of the method doStartTag() determines whether the body is evaluated, if there is no scriptlet or EL evaluation the body evaluation is just outputing the text in between the starting en ending tag. If there even is no text between beginning and ending tag it just returns from the method (with returncode SKIP_BODY)

Tag handler can extend IterationTagSupport

You mean TagSupport, yes there is no difference with the previous explanation. If you are extending the TagSupport class you don't even have to implement a doAfterBody() method as the default implementation returns SKIP_BODY (see my flow diagram on 8.5.2. The bold returncodes are the ones provided by the TagSupport default implementation)

implemented the doAfterBody() method,

Then it will run your implementation and you have to make sure you are continuing the flow (by returning SKIP_BODY)

doStartTag() method can return EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED

The EVAL_BODY_BUFFERED is only returned from the BodyTagSupport class (and it was not mentioned in the K&B statement). However, as the BodyTagSupport extends the TagSupport class it also implements the IterationTag and you can see from my flow diagram on ch 8.5.3 that it also works there: the doAfterBody() is called when the doStartTag() returns EVAL_BODY_INCLUDE.

but the doAfterBody() won't be executed if I have something like:
<pedro:myClassicTag />

It will unless you return SKIP_BODY from the doStartTag() method.

Make sure you learn the flows by heart for your exam and you also have to know it which interface the statics are defined!

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thank a lot Frits for your time! I've got your learning guide downloaded already but didn't go through it, as I want to finish the HF first (sounds weird - Head First first ;-)

But, I'm still not convinced.

Assume following directory structure and files:



web.xml


pedroTags.tld


PedroClassicTag.java



index.jsp


My result is:



When I change the index.jsp to the following one:



The result is:



So I still think that:
- a body-content set to something else than "empty" in the TLD,
- Tag handler extending TagSupport (or BodyTagSupport),
- implementing the doAfterBody() method,
- doStartTag() method returning EVAL_BODY_INCLUDE (or EVAL_BODY_BUFFERED in case of BodyTagSupport)

is not enought to execute doAfterBody(), as the tag itself cannot be empty.

Did I miss something?

Cheers!
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

My result is:
Tag: START!
Tag: END!

I am surprised here.....

Let me dig some what deeper into the jsp2.0 specs. The lifecycle transition diagram (jsp2.0, JSP.13.1.3) shows the possible transitions.

A little bit further the following is specified:
Empty and Non-Empty Action
If the TagLibraryDescriptor file indicates that the action must always have an empty element body, by a <body-content> entry of "empty", then the doStartTag() method must return SKIP_BODY.

Note that which methods are invoked after the doStartTag() depends on both the return value and on if the custom action element is empty or not in the JSP page, not on how it's declared in the TLD.

If SKIP_BODY is returned the body is not evaluated, and then doEndTag() is invoked.

If EVAL_BODY_INCLUDE is returned, and the custom action element is not empty, the body is evaluated and "passed through" to the current out, then doAfterBody() is invoked and, after zero or more iterations, doEndTag() is invoked.

Conclusion: You where right!!!

I am still learning things , this certification is full of subtle details...

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks Frits, now I guess my question can be treated as solved :-)

Cheers!
Rohini Sahuji
Greenhorn

Joined: Jul 06, 2007
Posts: 19

The result is:

Tag: START!
Tag: AFTER BODY!
Tag: END!


THis may sound stupid....
But my question is...
what happened to "!!!"
why its not printed?


SCJP
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Well, it is, but in the rendered JSP page.

I've posted only the System.out.println(-) statements, which prints to the TOMCAT_HOME/logs/catalina.out file.
Rohini Sahuji
Greenhorn

Joined: Jul 06, 2007
Posts: 19

Ohhhh...
Got It.
Sorry I dint see,
System.out.....

Thank you!
Benjamin En
Greenhorn

Joined: Aug 18, 2010
Posts: 13
Hey Guys!

I got hooked on the same question as pedro, and also assumed, that NONE of the given answers is right.
The spec seems to give him and me right.
So is it correct, that answer E shouldn't be checked?
And if yes, why isn't it listed in the errata yet?

Hope you can help me ;)


OCJP6
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

Hi !

So is it correct, that answer E shouldn't be checked?

We came to the conclusion that answer "e" is questionable. There is a possiblity that a Tag doesn't have a body and in that case the doAfterBody() won't be called at all... However from all the answers this one is the best....

And if yes, why isn't it listed in the errata yet?

I have no idea if somebody has mentioned it to the authors... maybe you want to do it

Regards,
Frits
Benjamin En
Greenhorn

Joined: Aug 18, 2010
Posts: 13
Well actually after thinking about it, Option E is correct.
Because if doAfterBody() is reached - which is the assumption of option E - returning EVAL_BODY_AGAIN will definitely let
doAfterBody() get invoked again.

So actually it is rather tricky than wrong.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

Well actually after thinking about it, Option E is correct.
Because if doAfterBody() is reached - which is the assumption of option E - returning EVAL_BODY_AGAIN will definitely let
doAfterBody() get invoked again.

I agree, but if your tag doesn't have a body it will never execute the first doAfterBody()... so to make the question less ambigious there could be a note that you have to assume that the tag is invoked from a jsp and has a body.

Regards,
Frits
Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

Frits Walraven wrote:
But the question does not tell you which interface/class is the tag handler implementing/extending. When you call a class "tag handler", does it mean that it is implementing the Tag interface?

Not always, you have to look at the methods they are giving you in the question. In this question they are only mentioning doStartTag() and no doTag() so you question is only about the "Classic" Custom Tags. (which all implement the Tag interface though). The Simple Custom Tag, doesnot implement the Tag interface.

Regards,
Frits


Firstly the EVAL_BODY_INCLUDE is declared in "Tag" interface not the iterationtag interface.
Can you now say the answer to this question?
It can be c) as well as e)



Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

Firstly the EVAL_BODY_INCLUDE is declared in "Tag" interface not the iterationtag interface.
Can you now say the answer to this question?
It can be c) as well as e)

Well spotted

I agree, c could be a good answer as well as long as your tag has a body when using it, so like:
when you use your tag like Pedro was saying without a body:then the doAfterBody() is not called. Therefore a well designed question will say that the tag under question has a body when used. Now it makes the answers questionable.

Regards,
Frits
 
wood burning stoves
 
subject: doAfterBody()
 
Similar Threads
Regarding Classic Custom Tags
One Tag Question
Custom Tag Qn
Doubt in doAfterBody
doAfterBody() doubt