Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

Scope of Session Attributes

 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
They don't.
The most common cause of this type of concurrency issue is the improper use of instance variables.
 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic