Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes HttpSession declaration scope Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "HttpSession declaration scope" Watch "HttpSession declaration scope" New topic
Author

HttpSession declaration scope

Kenny Dilger
Greenhorn

Joined: Jan 14, 2002
Posts: 11
At what scoping level should the decleration of an HttpSession be at? Is it okay be at the field level or should it be declared in either doGet() or doPost and passed along to methods needing access?
Thanks,
Kenny
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
you get your session by calling getSesison() on your HttpServletRequest object which is available in all doXxx() methods...


Asher Tarnopolski
SCJP,SCWCD
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
At what scoping level should the decleration of an HttpSession be at? Is it okay be at the field level or should it be declared in either doGet() or doPost and passed along to methods needing access?

It makes no sense to assign session objects to servlet fields (instance variables). Fields are shared by all users who access the servlet, whereas sessions are specific to a user. So, assign session objects to local variables only (reassign it on the next request -- the point is that request.getSession(true) returns the same object next time if it is the same client).
Or am I misunderstanding your question?
Cheers-
- Marty


Java training and consulting<br /><a href="http://www.coreservlets.com/" target="_blank" rel="nofollow">http://www.coreservlets.com/</a>
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: HttpSession declaration scope
 
Similar Threads
Cookies compression & decompression at client side
Obtaining session object from context
How to get session scope backbean
cannot find symbol error message
How to set the session attribute for the different jsp page data value.