File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet context vs session" Watch "servlet context vs session" New topic

servlet context vs session

Buk Lau

Joined: Nov 06, 2002
Posts: 4
When would you use one over the other and why?
Thanks in advance,
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
I agree. Here's the link:
subject: servlet context vs session
It's not a secret anymore!