File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Thread Safe Custom Tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread Safe Custom Tags" Watch "Thread Safe Custom Tags" New topic

Thread Safe Custom Tags

Jason Stortz
Ranch Hand

Joined: Jan 11, 2001
Posts: 68
Brace yourselves, this could get complicated:
The basic issue is this: I am concerned about the thread saftey of my Custom Tag. I have a tag that has one "property" called page. Hence, here is the basic outline of my custom tag:

Now, to call my tag in the JSP I use:
<tags:mytag page="hello">
The problem comes when I use this little plug in other pages and the "page" attribute is being set to all kinds of things. Is this thread safe? I am wary of this, and not convinced at all that when I call it with "hello" that I won't get back something else from a different call occuring at the same time.
Anyone offer any insight or fixes?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
The way I read the JSP 1.2 specification, objects created to implement taglibs are re-used just like servlet objects. Therefore you should not use instance variables like this.
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
As far as I am aware this IS thread safe. I am sure that a new Tag handler is created to handle EVERY request... instance variables should be fine.
Tag handlers are reused (one a request has finished with it!), but you SHOULD clean up for instance varaibles in the release() method if you rely on them being initailised correctly....

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Trevor Dunn
Ranch Hand

Joined: Jun 13, 2001
Posts: 84
As far as I undertand once a TagHandler is invoked then no other thread can acccess it untill it is finished, so you are safe. However they are reused so instance variables you may have set should be nulled in the doEndTag method. The resaon for this is if you call the tag again and you do not set the instance variables then they can have the values of some previous time that the tag was run and you could get some erroneous results.
Adam Chace

Joined: May 24, 2001
Posts: 14
Trevor is right. Your tag is thread-safe.
Adam Chace
Author of :JSP Tag Libraries
Chalk Creek Software

Adam Chace<BR>Author of :<A HREF="" TARGET=_blank rel="nofollow">JSP Tag Libraries</A><BR><A HREF="" TARGET=_blank rel="nofollow">Chalk Creek Software</A>
Jason Stortz
Ranch Hand

Joined: Jan 11, 2001
Posts: 68
Thanks for all your replies everyone. I really appreciate them. And it is a big relief to find out what I have. That makes things pretty easy and nice to deal with.
Thanks again.
I agree. Here's the link:
subject: Thread Safe Custom Tags
It's not a secret anymore!