Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which object is better for a Servlet Thread-safe implementaion? 1

Status
Not open for further replies.

royc75

Programmer
Jun 1, 2006
127
GB
Hello,

Suppose I would like to keep my attributes in a Servlet which suppose to be Thread-safe but it does not implement the SingleThreadModel.
Which is better: To store the attributes at the HttpServletRequest object (where it is per request, therefore will be kept for each user) or at the ServletContext object (Where it is shared globaly and one can be sure everyone are reading and updating the same data)?
 
Hey sedj,
Could you please explain why not? And what do you do when you need to share data globally? Do you use a Database instead?
I apologize for all these questions but there are things that you won't find in any text book unless you experienced with it yourself and I assume you already have thefore I am asking...
 
I really can't answer thes extremely vague questions until you define what kind of data you are referring to.

If you need to persist data beyond the lifespan of a session or visit ot a website then use a database.

What do you mean by share data globally ? To other JVMs ? To other classes in your webapp ? Just within your servlet ? Who is using this data, what is it being used for ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Let's say for example that you would like to share a certain String object between all the classes in your webapp (not other webapps and not other JVMs).
How would you do that?
 
Code:
public class WebappConstants {
   public static String str = "hello";
}

You can just do it like above. Declaring the string as static means there will only ever be one version of it.
All classes in the webapp will be able to access it.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Wait, but now it' not thread-safe so I assume every time you will use this String you will Synchronize it like this:
synchronize(str)
{
...
}
Am I correct?
 
No, you would add synchronized access to the holding class (WebappConstants)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
But in this case would'nt it be better (for performance) to create getter/setter methods for str inside the WebappConstants class and synchronize them instead of synchronizing the whole class?
 
Yes, thats what I meant - you apply synch ONLY to the read methods applicable within the holding class.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top