File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question 19 in mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question 19 in mock exam" Watch "Question 19 in mock exam" New topic

Question 19 in mock exam

L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37
A Classic tag handler exists in legacy code. The author wrote a handler that evaluates its tag body a hundred times, to be used in testing other tags that produce random content.


What is incorrect about the code?
A. Tag handlers are not thread safe, so the iterationCount can become out of sync if multiple users are reaching the page at the same time.
B. The doAfterBody method is never being called because it is not part of the tag handler lifecycle. The developer should have extended the IterationTagSupport class to include this method in the lifecycle.
C. The doTag method should be doStartTag. As written, the default doStartTag of TagSupport is called which simply returns SKIP_BODY, causing doAfterBody to never be called.
D. When doAfterBody returns EVAL_BODY_AGAIN the doTag method is called again. The doTag method resets iterationCount to 0, resulting in an infinite loop and a java.lang.OutOfMemoryError is thrown.

The Final exam answer in the book gives C, but I selected A and C.
This question is regarding the classic tag support. I know simple tag does not reuse tags, but classic tag dose. I ever wrote a snippet code with or without reset of the counter instance variable. The test confirms me that the classic tag reuse the same instance of the tag and keep the status of the instance variable in the tag.

Then why A is incorrect in the question? Thanks.

Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3470

Where is this question from? (please QuoteYourSources).

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37
The question is from Head First Servlet and JSP final mock exam.

Devaka Cooray wrote:Where is this question from? (please QuoteYourSources).
Ankit Garg

Joined: Aug 03, 2008
Posts: 9339

Yes instances of classic tags are reused, but they are not used to serve two requests at the same time. At a time, an instance of a classic tag will serve only one request. The container maintains a pool of instances of classic tags. When an instance of a classic tag is serving a request, its removed from that pool and returned to the pool when it finishes serving the request. Because of this reason, iterationCount is set to 0 inside of the doTag method instead of initializing it with its declaration...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
rich dubois

Joined: Feb 16, 2010
Posts: 20
Hi Ankit, thanks!

Great answer and it makes a lot of sense. It is something that has been bothering me since I read about these, I should have asked myself.

David, thanks for asking!
L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37
Thanks Ankit.
I agree. Here's the link:
subject: Question 19 in mock exam