Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scope of Session Attributes

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic