aspose file tools*
The moose likes Beginning Java and the fly likes Class Instance being overwritten by new Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Instance being overwritten by new Instance" Watch "Class Instance being overwritten by new Instance" New topic
Author

Class Instance being overwritten by new Instance

Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
I have written a class that store connection information for a user, such as UserID, Password, etc. This class is getting instanciated by my application at the point a user logs in. Everything works fine until another user logs in. Once this happens, the previous information is overwritten, as if using the same instance, instead of creating a new one. I am developing in JSP and am storing the class in the session variable.
Anyone have any thoughts on why this would happen or how to prevent it?
I am instanciating the class as follows:
db2Connect myConnection = new db2Connect();
I then do
session.setAttribute("connection", myConnection);
to store the connection object in my session.
I retrieve it using:
db2Connect myConnection = new db2Connect();
myConnection = (db2Connect) session.getAttribute("connection");
db2Connect is the class I wrote. The above is not copy/paste code, so ignore any syntax errors.
Thanks,
Jason.
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Is your new user logging in using a new web browser?
[This message has been edited by Mark Savory (edited March 30, 2001).]
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Jason,
It looks like your session variable (setAttributte method) is behaving like a java.util.Properties object. The java.util.Properties object extends from a java.util.Hashtable which stores key/value pairs. The keys are unique and if you insert the same key with a different value, then the last insert clobbers in previous value with the same key.
Here are some suggestions to fix your problem.
1. Don't use the setAttribute method.
2. Create your own map collection using the UserID as the key.
-Peter
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Yes, they are logging on with IE 5.
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
What I meant is: are the users logging in from separate browsers? I just wont to verify that there's a separate session object for each user.
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Yes, they are on another machine totally...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Instance being overwritten by new Instance
 
Similar Threads
I can't get the struts working
Stateful Session bean
Display struts output to jsp
Display struts output to jsp
WebLogic - SSL