• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference. between ServletContext and Session Attributes

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Arjun for giving me the difference...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic