Search code examples
jspjsp-tagslifecycle

JSP tag lifecycle


I just introduced a bug into my code because I seem to have misunderstood the jsp tag lifecycle.

The tag worked like this before the bug: I pass the tag some collection as an attribute, and it displays it as a table. The collection was passed into the JSP from the controller.

After the bug: I removed the attribute which set the collection. Instead, in the tag I check if the collection is null, and then grab it by name from the request (using a naming convention).

The thing that I didn't expect: after the collection was initially set in the tag, it would never become null on subsequent executions! It was still defined as a non-requred attribute in the TLD.

I expected the tag to not hold on to previous values between executions.


Solution

  • You answered the question yourself - it's pooled. See the tag tutorial for what to implement in java implementations, together with the page linked from there, containing the invocation sequence:

    ATag t = new ATag();
    t.setPageContext(...);
    t.setParent(...);
    t.setAttribute1(value1);
    t.setAttribute2(value2);
    t.doStartTag();
    t.doEndTag();
    t.release();
    

    That is, re-initialize your tag instance in doEndTag() as the API requires. (changed as of comment by Julien Kronegg, thanks)

    Note that pooling probably is container dependent, but well legal (and, due to the API setup, probably done everywhere).