• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safe Custom Tags

 
Jason Stortz
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
Jason
SCJP
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
S
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Trevor
 
Adam Chace
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trevor is right. Your tag is thread-safe.
------------------
Adam Chace
Author of :JSP Tag Libraries
Chalk Creek Software
 
Jason Stortz
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic