*
The moose likes Servlets and the fly likes servlet context vs session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet context vs session" Watch "servlet context vs session" New topic
Author

servlet context vs session

Buk Lau
Greenhorn

Joined: Nov 06, 2002
Posts: 4
When would you use one over the other and why?
Thanks in advance,
Buk
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
The ServletContext is shared throughout an entire context. You would store data in it that is not modified by user actions (unless you want the changes to be global)... DataSources, read-only caches, etc.
The Session object exists for each user coming through the context. It is useful for storing user state data such as a User object of some sort, or wizard-style temporary variables between pages, etc.
Try to put more data in the ServletContext rather than the Session context. Since each visitor has its own session, session objects can really build up memory usage.
Hope this helps.


- Chris Stehno, SCPJ
 
Don't get me started about those stupid light bulbs.
 
subject: servlet context vs session