File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Custom Tags & Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Custom Tags & Threading" Watch "Custom Tags & Threading" New topic
Author

Custom Tags & Threading

Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
I have written custom tags in several web apps without problems, but with my recent quest to understand threading issues in JSP/servlets, I have a concern...
For example, nested looping tags (where a list collection's elements - are passed down to descendant tags one at a time, with the counter being incremented in the parent tag) - I want to be sure that the counter and collection is not altered by another thread while looping for the original thread.
(I can post code if necessary)
To my understanding, the same tag object is almost never called by multiple threads at the same time. So would the collection (obtained from a pageContext scope - request or session) and counter values of the parent tag and descendant tags be thread-safe until the looping operation is complete?
[ April 21, 2002: Message edited by: Toby Davis ]

SCJP2 (1.4)
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
I believe that there are no thread issues to manage in a custom tag. All requests are given a new object.
Just remember to implement release() methods to reset all variables.
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
I think that there's two separate points here.
First of all is the thread safety of the collection that you are iterating over. As with Servlet/JSP development in general, you should be careful about accessing mutatable objects that are stored within the wider scopes such as session and application. This is because it is possible to have other pages/requests access and update the object and modify it.
With the tag handlers, the JSP spec says that a tag handler instance will only be used to service a single request at a time so in essence, tag handlers themselves are thread-safe. If you have nested iteration tags, two tag handler instances would have to be used.
Hope that helps
Simon
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Something to bear in mind is that a new tag handler instance doesn't have to be created each time - the container can choose to pool them.
Check out section 10.1 of the JSP spec for more details on this.
Simon
Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
Simon -
Thanks for the pointer. Found the appropriate part of 10.1 in the JSP 1.2 spec, "Tag Handler as a Container-Managed Object"... makes sense. This is a good read, by the way, answers other questions I was going to ask...
As to my application variables, one set these don't usually change during user interaction. To my understanding, I can have thread problems with session if there are multiple requests to the same session object (like with multiple frames).
Thanks again,
Toby
---
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Originally posted by Toby Davis:
To my understanding, I can have thread problems with session if there are multiple requests to the same session object (like with multiple frames).

Yep ... that's the one.
Simon
Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
Simon - had no idea you were one of the authors of the Professional JSP 2nd Ed. book (which I have) until I paid attention to your signature.
Got it originally because the index had concepts and ideas that pointed in the right direction, and use it as a good reference for examples and very good guidelines on "proper design". Thanks again, to you and the other authors.
---
[ April 22, 2002: Message edited by: Toby Davis ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom Tags & Threading