This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Servlets and the fly likes HttpSession declaration scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
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
 
jQuery in Action, 3rd edition
 
subject: HttpSession declaration scope
 
jQuery in Action, 3rd edition