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

Problem with HttpSession?

 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
When we put an Object into session just like:
HttpSession hs = req.getSession(true);

hs.setAttribute("",);
LoginSession lgs = (LoginSession) hs1.getAttribute("loginSession");
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your question ?
[ June 19, 2007: Message edited by: Satou kurinosuke ]
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
click the wrong button

*********************************************
HttpSession hs = req.getSession(true);
LogSession logSession = new LogSession();
String str = "strValue";
hs.setAttribute("loginSession",logSession);
hs.setAttribute("strObject",str);


LoginSession newlogSession = (LoginSession) hs.getAttribute("loginSession");
String newStr = (String)hs.getAttribute("strObject");

newlogSession.setName("changeName");//change newlogSession @1
newStr = "changeName";//change newStr @2

*********************************************
@1:if i change newlogSession,then logSession.name in HttpSession(hs) is changed too,Why?is logSession and newlogSession appoint to the same Object?
@2:but if i change newStr,str in HttpSession(hs) is not changed

i know that this is related to String pool,String Object is not the same with a normal Object:
*********************************************
LogSession logSession = new LogSession();
newlogSession.setName("changeName");//change the object

String str = "strValue";
str = "newStrValue";//create a new String object and drop the object "strValue";
*********************************************

is this means that the method (LoginSession) hs.getAttribute("loginSession") is the same to new LogSession(); ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

newlogSession.setName("changeName");//change newlogSession @1
newStr = "changeName";//change newStr @2

*********************************************
@1:if i change newlogSession,then logSession.name in HttpSession(hs) is changed too,Why?is logSession and newlogSession appoint to the same Object?
@2:but if i change newStr,str in HttpSession(hs) is not changed


#1) There is only a single LogSession object, and logSession and newlogSession both reference it. It doesn't matter which reference to that one object you use when you make a call to one of its methods.

#2) That's not quite the same issue as in #1. After the assignment, "newStr" references a different object. The object it referenced earlier will still be in the session.

A statement like "hs.setAttribute(name, object)" does not put the reference "object" into the session. Rather, it puts the object that "object" points to at that time into the session.

So the immutability of strings (or the string pool) does not come into play here.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A session stores your attributes in a List (or any other kind of Collection). So the following example illustrates the same behaviour :


You'll get a reference to your object, so if you change its state, it will be reflected in the Session too. Strings are immutable, so you cannot change its value directly.
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
[QB]A session stores your attributes in a List (or any other kind of Collection). [QB]


can i treat session as ArrayList?where can i find the src of Class HttpSession?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was only a wild guess. The spec leaves the container providers free to implement sessions the way they want. I only said that to illustrate your problem.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Kevin:


can i treat session as ArrayList?where can i find the src of Class HttpSession?


HTTP Sessions can be considered to be a datastructure like map which stores information/Objects (session attributes)in the key-value format and implements HTTPSession interface.

You can download the source of Tomcat and then see the implementation of HTTPSession interface in that.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i treat session as ArrayList?

No. A session contains something like a Map for its attributes, but the session object itself is not a collection.
where can i find the src of Class HttpSession?

HttpSession is just an interface, so the source wouldn't tell you anything that the javadocs wouldn't. The source of any servlet container (like Tomcat) would contain at least one class that implements the interface.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for confusing everybody with my damn ArrayList

I wanted to illustrate why the object was being updated, and not the String.
Please forget about Session working like an ArrayList. I should have said Collection instead. Actually, I shouldn't have said anything, as container providers are free to use whatever they want to implement it. Even toilet paper...
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinks all
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic