*
The moose likes Servlets and the fly likes ServletContext in the clustered environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext in the clustered environment" Watch "ServletContext in the clustered environment" New topic
Author

ServletContext in the clustered environment

geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
In the api , there is a setter method for the servletContext. In case of the distributed application, there is one ServletContext per JVM.

In case the attributes are set in a context, will it not cause discrepancy among other contexts(on other jvm's)?? Or there is a way they are replicated??
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
If you read the servlet specification, it will be clear to you that attributes stored in a ServletContext are not meant to be shared across different containers. The attributes are local to the VM in which they are created.


From Servlets 2.3 specification:

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 (See Chapter SRV.7, �Sessions�), stored in a database, or set in an Enterprise JavaBeansTM component.


"The trouble with doing something right the first time is that nobody appreciates how difficult it was!"
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
That means... if i want to share data, across the application.. and do not want to use listeners or populate some model class. I cannot use the context to share the data..
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi,

That means... if i want to share data, across the application.. and do not want to use listeners or populate some model class. I cannot use the context to share the data..


If you want to share the data among servlets you must use either session or database in a distributed envrionment. Because context exists in multiple JVMs.

bye for now
sat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext in the clustered environment