Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question 19 in mock exam

 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Given

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
Marshal
Pie
Posts: 4515
275
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is this question from? (please QuoteYourSources).
 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is from Head First Servlet and JSP final mock exam.

Devaka Cooray wrote:Where is this question from? (please QuoteYourSources).
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
rich dubois
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic