JSP tags and tag recycling

P

plm

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.

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

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 question is, is my understanding of the JSP spec w.r.t. tag
recycling wrong, or is struts wrong?

Thanks for insight,

Peter
 
J

John C. Bollinger

plm said:
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.
 
P

plm

Hello,

Thanks for your explanation.
I'm not quite sure what you mean by 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


What if a non mandatory attribute has default value "X", and has been
set to "Y" on the first use of the tag instance? If I do not set the
value of this attribute back to "X" for example in doEndTag, then what
happens when the tag instance is being used once more? Are you telling
that the JSP engine remembers the initial value of the attribute (i.e.
the default) and then restores it before using the instance again?

Regards,

Peter
 
J

John C. Bollinger

plm said:
Hello,

Thanks for your explanation.
I'm not quite sure what you mean by this:





What if a non mandatory attribute has default value "X", and has been
set to "Y" on the first use of the tag instance? If I do not set the
value of this attribute back to "X" for example in doEndTag, then what
happens when the tag instance is being used once more? Are you telling
that the JSP engine remembers the initial value of the attribute (i.e.
the default) and then restores it before using the instance again?

When the tag instance is reused, the attribute still has the value "Y",
and THAT IS WHAT THE JSP CONTAINER EXPECTS. In a reuse scenario, the
container is *required* to *avoid* reinitializing any attribute with the
same target value that it already was initialized to. Those details are
moot, however, because JSP specifically says that tag handler properties
exposed as custom action attributes may not be modified other than by
their Javabeans setter methods, and that use of those methods is
reserved for the JSP container. (See JSP 2.0, page 2-51; substantially
the same requirements apply to earlier versions of JSP as well.)

Do note that the spec also says that tag handlers may only be reused
among custom action occurrences where the same set of attributes is
specified (though not necessarily with the same values). Perhaps that
will assuage some of your concern.
 
P

plm

Aha, now I see, thanks to the reference to page 2-51. I was not aware
that reuse of tags is only allowed if the same set of attributes is
used. Indeed, the JSP 1.2 spec says the same on page 163.

I doubt if all JSP containers do the right thing though: one of the
documents of resin prescribes tag handler writers to reset all
properties to their default values after doEndTag because of possible
instance reuse.
 
J

John C. Bollinger

plm said:
Aha, now I see, thanks to the reference to page 2-51. I was not aware
that reuse of tags is only allowed if the same set of attributes is
used. Indeed, the JSP 1.2 spec says the same on page 163.

I doubt if all JSP containers do the right thing though: one of the
documents of resin prescribes tag handler writers to reset all
properties to their default values after doEndTag because of possible
instance reuse.

Considering that this is at odds with the JSP *specification*, I would
recommend that you do not do it, and that you file a bug report on Resin
if you find that it doesn't do the right thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top