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.
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...