This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes updating object in session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "updating object in session" Watch "updating object in session" New topic
Author

updating object in session

Nijeesh Balan
Ranch Hand

Joined: Oct 09, 2000
Posts: 116
Hi,
I have kept an object in session.
During the next call to the servlet i retrieve the object from session, update it.. but am not setting the object back in session.
But to my surprise when i retrieve the object in JSP i receive the updated object.
But the same doesn't work if i clear the files from Tools/Internet options
I think this has to do with some object references..
Plz clarify.
Thanks in Advance,
Nijeesh.


Thanks & Regards,<br />Nijeesh.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
U don't need to call setAttribute() to set the updated object into session. When u get the object from session using getAttribute u are getting the refernce to the object so any change made to the object using the reference will be reflected to the object present in session since both of them refer to the same object...

Origina
lly posted by Nijeesh BH:
Hi,
I have kept an object in session.
During the next call to the servlet i retrieve the object from session, update it.. but am not setting the object back in session.
But to my surprise when i retrieve the object in JSP i receive the updated object.
But the same doesn't work if i clear the files from Tools/Internet options
I think this has to do with some object references..
Plz clarify.
Thanks in Advance,
Nijeesh.


Groovy
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Pradeep --
I don't think that's quite right. Check out this simple series of JSPs -- you'll see that with both variables I change their value, but only ONE I update the session -- this is the only one who's new value is persisted in session to the next page:
index.jsp:

updateSession.jsp:

displaySession.jsp:


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Nijeesh, presumably Tools/Internet Options & clearing files is clearing Internet Explorer's cache? I see no reason why doing that should affect what happens to the values in your session.
So what Jessica's code is doing is getting complete copies and not references.
Nijeesh, is your situation complicated or can you reduce it to a simple demo and post it?


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
I completely agree with what Adam says - What Jessica's code is doing is getting complete copies and not references. this is because ur using Strings and Strings are immutable objects..!
U cannot change the value!
When u say
blah = "5678";
now blah is pointing to entriely new object.. but the session object points to "1234".
A better example will be to use vectors and then u will notice the difference..In this case u don't need to explicitly call SetAttribute ..just call getAttribute and make some changes to the obtained reference and u find for sure that changes will be reflected in session object !

Originally posted by Adam Hardy:
Nijeesh, presumably Tools/Internet Options & clearing files is clearing Internet Explorer's cache? I see no reason why doing that should affect what happens to the values in your session.
So what Jessica's code is doing is getting complete copies and not references.
Nijeesh, is your situation complicated or can you reduce it to a simple demo and post it?

\
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updating object in session