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 Question about Session object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Question about Session object" Watch "Question about Session object" New topic
Author

Question about Session object

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I can't believe I am having to ask this question as much as I have been doing J2EE, but since there is no such thing as a dumb question on Javaranch....

If I store an object in the session and then later retrieve that object, if I make changes to the object, do I need to put it back in the session or am I working with a reference to the session object?

Thanks.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

You are working with a reference. No need to put it back. Think of the attributes as Map entries.
[ February 11, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Bear Bibeault:
You are working with a reference. No need to put it back. Think of the attributes as Map entries.

[ February 11, 2005: Message edited by: Bear Bibeault ]


Perfect, thanks. That saves me a bit of work.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Bear Bibeault:
You are working with a reference. No need to put it back. Think of the attributes as Map entries.

[ February 11, 2005: Message edited by: Bear Bibeault ]


Better yet, think of it as if you are coding in Java! All objects are passed by reference in Java! :-)


James Carman, President<br />Carman Consulting, Inc.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
We all have our DOH! moments Greg, nothing to be ashamed of.
Sometime last year I was looking desperately for why something I set in one JSP suddenly started causing errors in another.
Wasn't until a few days later that I realised I used the same session variable in 2 JSPs/servlets to hold quite different things...


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Does anyone have team practices to avoid what Jeroen described? A list of variable names in use, naming conventions? Seems like a very easy and probably very common mistake.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I generally bind only one object to the user's session: 'userBean'.
Anything I need to store in session becomes a property of that object.

If while working on component 'B', I decide to add variable 'x' to session, but have already done so for component 'A', I would quickly be reminded as soon as I tried to add property 'x' to the userBean object.

I never thought of it as a wrapper for the session, but I guess that's what it is, sort of, in reverse -- a more strongly typed session object. It wasn't written for this purpose.
I wrote it to avoid making repeated getAttribute calls with all the casting invloved (IOW: to save typing).

[ February 12, 2005: Message edited by: Ben Souther ]


You could enforce this policy in your code during development by using HttpSessionAttributeListener, Class.isInstance(Object), and an assertion to test any object being bound to the session to insure that it is an instance of yourpackage.userBean.
[ February 12, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I use an object called Visit which stores most of my session variables. So I only need to get the visit object to get everything else. Visit represents a users session on my web app. So for my current app I have objects set in Visit like user, currrentIssue, etc. I never have liked the idea of have 20 different objects seperatly in the session. I also use a constant I use when saving and retrieving my visit object so I don't have to wonder a year from now what I called it in the session and have to dig through my code. So I would do something like:


[ February 12, 2005: Message edited by: Gregg Bolinger ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by James Carman:


Better yet, think of it as if you are coding in Java! All objects are passed by reference in Java! :-)


...which I understand. But I wasn't sure if an HttpSession object worked the same way since it's a server side thing.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Originally posted by Stan James:
Does anyone have team practices to avoid what Jeroen described? A list of variable names in use, naming conventions? Seems like a very easy and probably very common mistake.

Yes. We have an interface where we define all the names of things stored in the session. (Yeah I know, a constant interface...) The person who starts using a new variable enters it in the interface. If it is already there, the name is in use and would cause all sorts of conflicts.

For larger projects, we have an interface for each tab (part of the application.) The constants in each interface all start with a common prefix to avoid conficts between tabs.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
So you say putting back those values all these years was in vain ?

I am so depressed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Session object