1

I have my application deployed in a cluster with two servers. When I set any attribute using:

this.getServletContext().setAttribute("test", testObj);
  1. Will testObj attribute with key test be available to access via both the servers?
  2. Does number of servers equal to number of JVM's? In my case there are two servers so does that mean there are two JVM's?
  3. As per my understanding ServletContext is one per webapp per JVM, so in my case will the testObj be available when accessed via both the JVM's?
Community
  • 1
  • 1
Deena
  • 297
  • 2
  • 7
  • 17

1 Answers1

2

According to the ServletContext javadoc:

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

The Servlet specification also states in "SRV.4.4.1 Context Attributes in a Distributed Container":

Context attributes are local to the JVM 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.

So the answer to you question is no, the attribute is not accessible in the entire cluster, it is available only on the local JVM.

As for the number of JVM's per server, in a basic setup you can have a JVM per physical machine but you could also have more JVM's on the same machine depending on the characteristics of the application or if you want to maximize your hardware usage.

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61