File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem with HttpSession? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with HttpSession?" Watch "Problem with HttpSession?" New topic
Author

Problem with HttpSession?

Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
Hi all:
When we put an Object into session just like:
HttpSession hs = req.getSession(true);

hs.setAttribute("",);
LoginSession lgs = (LoginSession) hs1.getAttribute("loginSession");


SCJP 5.0 72%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What's your question ?
[ June 19, 2007: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55

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.


Ping & DNS - my free Android networking tools app
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jul 24, 2006
Posts: 43
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jul 24, 2006
Posts: 43
Thinks all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with HttpSession?