File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Session v Servlet Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session v Servlet Context" Watch "Session v Servlet Context" New topic

Session v Servlet Context

Gagan Sabharwal
Ranch Hand

Joined: Apr 23, 2006
Posts: 48
Hello !!!
Since servlet is something new for me!!! I am finding some difficulties to deal with it....

How are the sessions more advantageous than servlet contexts? I wanted an illiustration as well...So,i would be really thankful if any of u guys help me out of this!!!

[Edit by Dave to give a meangful subject]
[ April 25, 2006: Message edited by: David O'Meara ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Please use a meaningful topic, please use real words.

A session is related to a user, so each time the user provides a request you can look at the information attached to this user. A Servlet context is attached to a Servlet, and the same servlet serves multiple users. Therefore the two are not the same.

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Gagan Sabharwal:
How are the sessions more advantageous than servlet contexts?

Sessions belongs to a particular user whereas servletContext is shared by all active users. We store all user related info to the session of that particular user like, role etc. Whereas servletContext is used to store appication level information.

Session remains valid only as long as the users browser stays open and user remains active.

Edited after Bear's post
servletContext remains valid until application goes out of service.
[ April 26, 2006: Message edited by: Adeel Ansari ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Just to make sure that there are no misconceptions...

servletContext should have been more properly named applicationContext. There is only one servletContext for the entire web app. It is not tied to any one particluar servlet. It comes into scope when the application is started, and stays in scope until the application is stopped. The servlets themselves can come into and out of scope and the servletContext is not affected.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Session v Servlet Context
It's not a secret anymore!