Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet context vs session

 
Buk Lau
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When would you use one over the other and why?
Thanks in advance,
Buk
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic