Sharing Data Between Sessions, WARs and Context

As per servlet specification the HTTP Session (which is normally used to share data between request) should not be shared across WAR or Context due to security reasons.

There are several other way through which you can share the data betwwen Sessions, WARs and Context. These options should be used on risk.

Here are few options I have worked on.

Sharing between Context using Tomcat HTTP Server

Tomcat provide a context configuration xml, in which you can set the value of 'crosscontext' attribute to 'true'. This will share the context between WAR's. Using this approach we can access another context from a context.

Following are the code you have to use for accessing the context,



Say you have two context namely WebApp1 and WebApp2.

Now set the value of cross-context as true for WebApp1 by providing context.xml
For details visit Tomcat Context


In WebApp1, you can set the data to the context as context.setAttibute("DATA",object).

In WebApp2, instead of accessing it's context object, get the Context of 'WebApp1' as,
ServletContext fcontext = currentContext.getContext("WebApp1");
and then fcontext.getAttribute("DATA").

If the cross-context value is false, the value of fcontext will be null.



Using JBoss Cache to Share Data

JBoss Cache provide two ways of caching. One by using MBeans and second by creating the cache programatically.

Following link explain the details of using JBossCache (TreeCache).
Tree Cache - JBoss

For any more details, please feel free to write to me at biju74@yahoo.com