jQuery in Action, 3rd edition
The moose likes JSP and the fly likes JSP tags retaining previous data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP tags retaining previous data" Watch "JSP tags retaining previous data" New topic

JSP tags retaining previous data

Jag Bains
Ranch Hand

Joined: Nov 19, 2002
Posts: 39
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?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

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?

[Asking smart questions] [About Bear] [Books by Bear]
Gary McGath
Ranch Hand

Joined: Mar 15, 2003
Posts: 52
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
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

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.
[ March 28, 2003: Message edited by: Bear Bibeault ]
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
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!
I agree. Here's the link: http://aspose.com/file-tools
subject: JSP tags retaining previous data
It's not a secret anymore!