What is the result of accessing the following JSP document? (Choose one.) JSP document: <html xmlns:mytags="http://www.osborne.com/taglibs/mytags" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" > <jsp utput omit-xml-declaration="true" /> <jsp irective.page contentType="text/html" /> <jsp irective.page import="java.io.Writer" /> <body>
For me, "doAfterBody" is not supposed to be processed, since it's a Classic tag, and the default value for doStartTag is "SKIP_BODY".
You are incorrect - not overriding doStartTag() in TagSupport would skip the body; but in BodyTagSupport, this method returns EVAL_BODY_BUFFERED, so in fact nothing goes wrong there, and the question is correct from that perspective.
The question is also correct because the tag uses a buffer; doInitBody() is invoked first and starts writing "Head" to the buffer. Then the body content of the tag is invoked, causing the scriptlet to write "Body" directly to 'out', then the doAfterBody() writes "Legs" to the buffer. The final buffer content is "HeadLegs", so when it is appended to 'out' in doAfterBody(), the final output becomes "BodyHeadLegs".
Ah, and another thing...(doAfterBody shouldn�t be returning "EVAL_PAGE" since it�s "doEndTag�s method". Would it be a runtime exception right?
You are correct, it should return EVAL_BODY_AGAIN to evaluate the body once more, or SKIP_BODY to jump to doEndTag(). However, it may not cause a runtime exception, because these constants are numerical, so if the numerical value of EVAL_PAGE matches with the numerical value of a valid constant, everything will still work (perhaps not correctly). EVAL_PAGE actually has the value 6, while no other valid value for doAfterBody() does, so in fact you would get some sort of problem here... However, the value of constants should never be relied upon (and I wouldn't be surprised to see this replaced with a type-safe enum in a much later release).
I also have a reservation here because doAfterBody() never seems to return SKIP_BODY. What would actually happen in practise is the body would be evaluated infinitely, and probably end up with a container stack overload, and a restart of the Web server! At some point you must return SKIP_BODY from doAfterBody() to end the tag's invocation. From this perspective, I think the actual answer would be "BodyHeadLegsBodyLegsBodyLegs..." ad infinitum. [ April 16, 2006: Message edited by: Charles Lyons ]
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: Feb 18, 2005
Great Charles Tks.
U�re right, The doStartTag really returns "EVA_BODY_BUFFERED" intead of "SKIP_BODY" for BodyTagSupport classes, i din�t pay attention it...
But another question regarding the answer.
Why does "body" appear before "Head"? because it�s a scripting?
Because "doInitBody" is supposed to be executed before processing the body, ins�t it?
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com