• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JSP tags retaining previous data

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm using jsp tags and the I need to over come is that the constructor of the tag is only called once and the tag object is then reused over and over again.
My tag has an attribute that is optional, so if the attribute was set during its use for 1 iteration but not required for the next one the object is used, the data remains in the tag.
Does anyone know how to get around this?
Jag
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What container are you using? Tomcat? Resin?
The behavior of the various containers around reuse of tag classes is quite different.
Resin, for example, very aggressively re-uses tag instances, but is very careful to make sure that the problem you describe does not occur. Other containers may be less fastidious.
Are you really experiencing this issue, or are you just surmising that it might occur?
hth,
bear
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a messy issue which I had to deal with, with some help from the people on the Tomcat mailing list. If you do things correctly, they will work with all engines.
First: tags can be re-used, and you can't always predict when they will be re-used. Your release() method will be called when the tag instance is completed, and should reinitialize your tag attributes. Don't use the doEndTag() method for this purpose; that's the mistake which I was making.
If you use standard setter methods to set your attributes and release() to clear them, you should be fine.
 
Bear Bibeault
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't use the doEndTag() method for this purpose; that's the mistake which I was making.


Yeah, I learned that too the hard way about nine months ago. Ouch!
In any case, be sure that you aren't doing any clever caching of values or anything along those lines, and that your tag doesn't rely on instance variables that are orthogonal to tag attributes.
And never, ever call release() yourself.
If you treat the setters as straight-forward bean setters as Gary stated, the container should take care of creating instances and calling the setters in the correct sequence.
I've heard that weblogic has a really bad custom tag engine in this respect, but that's only heresay so take it with a grain of salt.
hth,
bear
[ March 28, 2003: Message edited by: Bear Bibeault ]
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Bear, you shouldn't actually need to reset your tag attributes because the container will do this for you, based around the rules of tag handler reuse.
However, you should remember to reset instance variables that your tag uses, but aren't set through the attributes of your tag. For example, if you tag uses an iterator, then make sure that you set/reset this before use. I would recommend doing this in the doStartTag() method since this is guaranteed to be called every time.
JSP 2.0 fixes all of this by introducing simple tags that are created and only ever used once. Hurray, no tag reuse issues!
Simon
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic