aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference. between ServletContext and Session Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference. between ServletContext and Session Attributes" Watch "Difference. between ServletContext and Session Attributes" New topic
Author

Difference. between ServletContext and Session Attributes

Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
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.


Arjun
Tom Arons
Greenhorn

Joined: Jan 04, 2002
Posts: 10
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

Joined: May 24, 2001
Posts: 25
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

Joined: Feb 02, 2011
Posts: 81
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
Bartender

Joined: Apr 07, 2010
Posts: 1678
    
  25

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

Joined: Feb 02, 2011
Posts: 81
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
Bartender

Joined: Apr 07, 2010
Posts: 1678
    
  25

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

Joined: Apr 20, 2011
Posts: 21
Thank You Arjun for giving me the difference...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference. between ServletContext and Session Attributes