Re: JSP tags and tag recycling

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 03/14/05


Date: Mon, 14 Mar 2005 11:47:03 -0500

plm wrote:

> Hello,
>
> I have always believed that due to the possibility of tag recycling,
> that is the JSP engine may reuse tag instances (the only guarantee you
> have is that no multiple threads are inside a tag instance at the same
> time), one has to reset all non-mandatory attributes to their default
> values in doEndTag(), that is before the tag may be used again.
>
> I have seen various recommendations to do this.

It depends on the details, but such recommendations are typically flat
wrong. In part _because_ of tag reuse, it is *forbidden* by JSP for any
tag handler state exposed as an attribute, whether optional or required,
to be modified by any means other than that attribute, as provided by a
page implementation using the handler. Furthermore, it is (or should
be) unnecessary to reset tag parameters that have not been changed from
their default values. That means that you shouldn't ever touch tag
parameters programmatically, except at instantiation and release.

> However, all Struts tags do something else, something strange: they
> reset the tag attributes to their default values in the release()
> method, which is meant to release any resources (such as JDBC
> connections etc) a tag instance might have. AFAIK an attribute value is
> not a resource :).

What if the value is a JDBC Connection? Or a large Collection?

> If I'm not wrong, release() is only called when the tag instance is
> destroyed, not between reuses of the current tag instance. So the
> struts way of doing things might lead to unexpected results, where tags
> attributes of reused tags do not have their documented default values.

The release() method is guaranteed to be invoked when the JSP container
is done with a tag handler instance, before that instance becomes
eligible for GC. If Struts provides some kind of level 2 caching for
tag handler instances, or otherwise has to worry about instances hanging
around past their usefulness, then it makes sense to reset attributes in
release(). Otherwise it is pointless (but not wrong).

As for tag handlers and attribute values, it is precisely when programs
violate JSP's restrictions about mucking with tag handlers' attributes
that problems may arise. In code written as you seem to prefer, the
problems would tend to take the form of a tag attribute having its
default value when it was supposed to have a customized one.

> The question is, is my understanding of the JSP spec w.r.t. tag
> recycling wrong, or is struts wrong?

Your understanding is wrong. Struts might be doing unnecessary work,
but you are proposing non-compliant code.

-- 
John Bollinger
jobollin@indiana.edu


Relevant Pages

  • Re: confused about custom tags keeping state
    ... the doStartTag and doEndTag methods can be invoked on the tag ... After the doEndTag invocation, the tag handler ... > is available for further invocations (and it is expected to have retained ... > I normally define my own destructor or initialiser method which I invoke in ...
    (comp.lang.java.programmer)
  • Re: Are custom tags thread-safe?
    ... Most tag creation is little more than memory allocation, and therefore pooling is of dubious benefit. ... The reuse of tags is not done through weak reference or other uses of the garbage collector, so the JSP implementation has no reasonable way of detecting abuses. ... Because of potential reuse of tag handler instances, you should also be sure to adhere to the rules for tag handler behavior that you will find in the JSP specs. ... Relative to thread safety, however, Tom already covered the main point, which is that the servlet spec promises that a tag handler is assigned exclusively to servicing one request at a time. ...
    (comp.lang.java.programmer)
  • Re: Hard coding HTML into JSP tag handler
    ... > my .tag file from within my tag handler class? ... > the reverse case (using the tag handler from within the .tag file since ... The way to solve your general problem is to pass the additional JSP ...
    (comp.lang.java.programmer)
  • Re: confused about custom tags keeping state
    ... The documentation for the Interface Tag states "There are two main actions: ... the doStartTag and doEndTag methods can be invoked on the tag ... After the doEndTag invocation, the tag handler ... is available for further invocations (and it is expected to have retained ...
    (comp.lang.java.programmer)
  • Re: Hard coding HTML into JSP tag handler
    ... my .tag file from within my tag handler class? ... the reverse case (using the tag handler from within the .tag file since ... For example I would like my tag handler class to do the business logic ...
    (comp.lang.java.programmer)