Version 13

    Sharing Session Data Across WARS


    Although sharing session data across WARS is a Servlet 2.4 spec violation, and a potential security issue, there is a semi-reasonable business case for it:


    A large application is broken up into multiple WARS, so that new functionality can be pushed out in a more modularized fashion.


    While there is a more elegant way to handle this via JBossCache, it is possible to get JBoss 4.0.2 to share session data across WARs. I used as a reference for sharing session data across WARs.


    WARNING - I was not able to get this to work in Tomcat 5.5.7 or Tomcat 5.5.9 standalone. Sharing of session data across WARS is a violation of the Servlet 2.4 spec and this workaround should be considered a last resort!


    In a nutshell, this is what I did:

    • Created a simple struts app/configuration for my test JSPs and Actions.

    • Write a method that stores session data in a "shared" context.

          private synchronized void storeDataInContext(ServletContext context, String sessionid, Object data) {          
             //load the shared_userroles from the context
             Hashtable shareddata = (Hashtable)context.getAttribute("shared_data");
             // if not yet available, create a new one
             if (shareddata==null) {
                shareddata = new Hashtable();
             // store the userroles of the current session
             shareddata.put(sessionid, data);
             // store the shareddata back into the Context
             context.setAttribute("shared_data", shareddata);
             System.out.println("I SET THE DATA OBJ TO: " + data);
    • Write method that gets data from the shared context.

          private Object getDataFromContext(ServletContext context,     String sessionid) {
             // get the Context containing the shared session data
          ServletContext ssocontext = context.getContext("/wars-session-share");
          Object data = null;
          // read the shareddata
          Hashtable shareddata = (Hashtable) ssocontext.getAttribute("shared_data");
             if (shareddata != null) {
              // get the right object using the ssosessionid
              data = shareddata.get(sessionid);
          return data;
    • Make sure jbossweb-tomcat55.sar\META-INF\jboss-service.xml has UseJBossWebLoader="true". Otherwise, you will get a ClassCastException.



    I've attached the source, so you can try this approach. I've included a simple ant build.xml script that'll compile/deploy the code to jboss or tomcat. I don't know why it doesn't work in standalone tomcat, but I'm pretty sure the issue is configuration related.