aspose file tools*
The moose likes Servlets and the fly likes Scope of Session Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Scope of Session Attributes

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I thought from my earlier post I had the scope of the session attribute understood.

I have a web app located on the web server. When a user logs on several session attributes are set and on a main menu page a message displays their name.

Today I found out that if one user has logged in everything appears to be ok. But when a second user logs on the fisrt users main menu message will display the second users name and it appears that all the session attibutes for the first user is changed to the second users log on criteria.

Please help I thought that each user logging on instanciated totally different and separate session.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

They don't.
The most common cause of this type of concurrency issue is the improper use of instance variables.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Here is my servlet code(please don't laugh remember this is web development is new to me and I am learning every day):

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61762
    
  67

No laughter, everyone was a n00b at this at one point.

Even though each user has their own session, there is only one instance of the servlet. You are using class and instance variables in the servlet. Bad bad news! Remove all class and instance variables and store temporary information in variables within the methods, and permanent info in the session.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

These are the instance variables:


Since there is only one instance of this servlet, all requests share, update, and read from the same variables.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Originally posted by Bear Bibeault:
No laughter, everyone was a n00b at this at one point.

You are using class and instance variables in the servlet. Bad bad news! Remove all class and instance variables and store temporary information in variables within the methods, and permanent info in the session.


From my code posted can you give me some specifics. I am thinking the MiscObjects might be one problem. Here I think each session would need to instanciate a new MiscObjects. Is this the sort of thing you are talking about?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61762
    
  67

What Ben posted. NO instance or class variables!

Disregarding the servlet-ness for a moment, you also have a bigger problem with general Java concepts. Many of your variables are declared public. Generally variables should be private. And what's with the static variables?

In a non-servlet class this is going to cause you no end of issues.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In short, yes, moving your variable declarations inside your doPost/doGet methods will take care of the problem you originally reported.

A good rule of thumb is to not never use instance variables (or static class level variables) in servlets unless you
A) really know what you're doing and
B) have a very good reason to do so

If the compiler is warning you that a variable might not have been initialized it's because it's possible for the part of the code that uses that variable to be reached without going through the code that initializes it.

Example:



On any day but Wednesday, the code on line 4 will never be executed so
the name variable will be undeclared by the time line 7 is reached.

Look for a similar situation in your servlet code.
[ January 25, 2008: Message edited by: Ben Souther ]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Ok I think I have the variable problem solved. But now my JSP is giving an error:

Exception thrown : java.lang.ClassCastException: java.lang.String

It is the bean that is supposed to display a message.


[ January 25, 2008: Message edited by: Steve Dyke ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Steve, this sounds like a different issue.

It would be better to start it in a new thread.
This one is already long and many people will get annoyed to find out that, after reading 8 or 9 posts with lots of code, that the topic changed and that all their time reading the beginning was a waste.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope of Session Attributes