Hi All, I have the following code in my javaservlet application :
If I use the setAttribute and getAttribute methods to store data in the session, is that data shared with all clients who access the application running under tomcat ? I had thought if I stored something in the session that it was only used by that particular client, but I am starting to see behaviour which indicates that other clients accessing the web app are using that data.
I think I am misunderstanding something global here.
What is happening is I have a table of data and I click on a column heading to re-sort the data based on that column. I save the sort column to the session so when the table auto-refreshes it knows how to sort. Well, another client running on a seperate PC autorefreshes and now has that same sort order as the first client.
a HttpSession is specific to one particular client. The sessions are maintained via cookies or URL rewriting if cookies are disabled.
The strange behavior you're encountering may be because you have multiple browsers windows opened on one machine or multiple tabs in own browser window. This depends on the browser if it's possible to determine it as different sessions. Generally the attributes in session scope should not be accessible to other clients!
Marco [ June 03, 2008: Message edited by: Marco Ehrentreich ]
Hmm, what I have is a java-struts web app running on a server under tomcat. Then I have a client using IE 7.0 on client machine A. I also have a client using IE 6.0 on client machine B. When I sort on machine B in the 6.0 browser, the 7.0 browser on machine A resorts when it hits its refresh time (every minute).
I wonder if when I start up the second browser, on machine B, I am getting access to the session that started with machine A ? I do use this code in one of my classes,
Could that be doing it? I pass the false so I can see if the session has expired and I need to make the user log in. I thought this would only check on the session per this client, but maybe it will go and find any open session?
Thank you so much for your reply. I have been reading other similar posts on JavaRanch and have seen the mention of using instance variables in servlets, and I do (I have an object that holds some static data), but not this particular sort variable. It is not saved as as an instance variable in the servlet, it is saved to a struts form variable, so not sure how this is happening.
Hey, I was thinking about static data when you said "instance variables", but taking a second look at my struts action class, I do have a data member in this action class that holds the sort field, would this be an example of what you mean that could be causing me woes ?
like Bear already said all member variables (static or non-static) which are shared between multiple threads without correct synchronization are subject to such subtle errors or misbehaviors.
For Java web applications there's even multi-threaded access (for some objects) if you don't explicitly create multiple threads because this is done by the servlet container (Tomcat) for obvious performance reasons.
Unfortunately I don't know the architecture and concepts of Struts, so I can't tell you if this DispatchAction could be shared between multiple threads. But the problems you've described seem to be very good candidates to multi-threading issues.