aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread Safe Custom Tags" Watch "Thread Safe Custom Tags" New topic
Author

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?
Thanks,
Jason
SCJP
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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

Java Resources at www.wbrogden.com
Sam Dalton
Author
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....
S

<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" 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.
Trevor
Adam Chace
Greenhorn

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="http://www.amazon.com/exec/obidos/ASIN/193011009X/electricporkchop/102-2552103-3190518" TARGET=_blank rel="nofollow">JSP Tag Libraries</A><BR><A HREF="http://www.chalkcreek.com" 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.
Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Safe Custom Tags
 
Similar Threads
JSP EL not working
Bean Vs. Custom Tag - Does this make sense?
some important mock exam experiences !!!
Tutorial on Dynamic Attributes
problem with Custom Tags