File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference. between ServletContext and Session Attributes

 
Arjun Anand
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Both ServletContext and HttpSession objects can hold attributes. According to Servlet 2.3 spec, the difference between the attributes in ServletContext and HttpSession is that

SRV.3.4.1 Context Attributes in a Distributed Container
Context attributes are local to the VM in which they were created. This prevents ServletContext attributes from being a shared memory store in a distributed container. When information needs to be shared between servlets running in a distributed environment, the information should be placed into a session, stored in a database, or set in an Enterprise JavaBeans component..

Is there any other difference between ServletContext and HttpSession attributes which i'm missing?

Also in Servlet Spec 2.3 in Chapter 10, Application Lifecycle Events it states,

SRV.10.6 Distributed Containers
In distributed web containers, HttpSession instances are scoped to the particluar VM servicing session requests, and the ServletContext object is scoped to the web
container’s VM. Distributed containers are not required to propogate either servlet context events or HttpSession events to other VMs. Listener class instances are scoped to one per deployment descriptor declaration per Java virtual machine.


Here it says the Session is scoped to the particular VM servicing the session requests which means I cannot use sessions to store attributes to be shared, in a distributed container.
This contradicts the previous one. Which one is correct? Somebody throw lights in this.
 
Tom Arons
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think these specs are maybe a detailed explanation to say that sessions can migrate to other VM's. Each VM has a separate instance of ServletContext and so do not rely on state information in ServletContext.
 
Arjun Anand
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Arons:
I think these specs are maybe a detailed explanation to say that sessions can migrate to other VM's. Each VM has a separate instance of ServletContext and so do not rely on state information in ServletContext.

That is what even I thought. But wanted to confirm if my understanding is right.
 
Cristian Daniel Ortiz Cuellar
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Anand wrote:
Originally posted by Tom Arons:
I think these specs are maybe a detailed explanation to say that sessions can migrate to other VM's. Each VM has a separate instance of ServletContext and so do not rely on state information in ServletContext.

That is what even I thought. But wanted to confirm if my understanding is right.

can Somebody give a example of ServletContext Attributes??'
i understand ServletContext parameters maybe i am wrong..

thanks..
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can Somebody give a example of ServletContext Attributes??'

Think of the ServletContext as a global space where you can add objects to (and read objects from) that should available to all the Servlets. So basically any object you would like to be available can be added to the ServletContext as an attribute.

ServletContext init parameters are those parameters that you define in the web.xml

Regards,
Frits
 
Cristian Daniel Ortiz Cuellar
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frits Walraven wrote:
can Somebody give a example of ServletContext Attributes??'

Think of the ServletContext as a global space where you can add objects to (and read objects from) that should available to all the Servlets. So basically any object you would like to be available can be added to the ServletContext as an attribute.

ServletContext init parameters are those parameters that you define in the web.xml

Regards,
Frits

Thanks Great.. you mean servlets.. you mean (Servlets) As JSP View... i mean servletContext in a JSP as well right..
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean servlets.. you mean (Servlets) As JSP View

Yes all Servlets and all JSPs (a JSP is a Servlet)

Regards,
Frits
 
aatish pandya
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Arjun for giving me the difference...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic