Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

updating object in session

 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

\
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic