JSP tags and tag recycling

Discussion in 'Java' started by plm, Mar 14, 2005.

  1. plm

    plm Guest

    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
    plm, Mar 14, 2005
    #1
    1. Advertising

  2. 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
    John C. Bollinger, Mar 14, 2005
    #2
    1. Advertising

  3. plm

    plm Guest

    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
    plm, Mar 14, 2005
    #3
  4. plm wrote:

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


    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.

    --
    John Bollinger
    John C. Bollinger, Mar 14, 2005
    #4
  5. plm

    plm Guest

    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.
    plm, Mar 14, 2005
    #5
  6. plm wrote:

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

    --
    John Bollinger
    John C. Bollinger, Mar 15, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    3
    Views:
    863
    Michael Borgwardt
    Jan 9, 2004
  2. Lukasz Indyk
    Replies:
    1
    Views:
    1,456
    Andrew Thompson
    Sep 22, 2004
  3. koslows
    Replies:
    0
    Views:
    523
    koslows
    May 12, 2005
  4. shruds
    Replies:
    1
    Views:
    757
    John C. Bollinger
    Jan 27, 2006
  5. johkar
    Replies:
    4
    Views:
    481
    johkar
    Mar 13, 2007
Loading...

Share This Page