Search code examples
javaspringtomcatservletsjava-ee-6

Sharing application scope data data between web applications?


I am using below to access the session data set by App1 in App2.

in App1:

ServletContext sctx = ((HttpServletRequest) request).getSession().getServletContext();
    String testStr = (String) sctx.getAttribute("attr");
    if(testStr == null){
        testStr = "test";
        sctx.setAttribute("attr", testStr);
    }

in App2

I get session value as below.

ServletContext sctx = ((HttpServletRequest) request).getSession().getServletContext().getContext("/app1");
    String testStr = (String) sctx.getAttribute("attr");
    System.out.println("the value which set in first app: " + testStr);

Actually my App2 will be in cluster environment. In this case should App1 will also be in cluster environment? My App2 is a web application which has few web services exposed and it does not contain any UI. There must not be any direct communication between App1 and App2. App1 has to put some value in Application scope on its start up and later App2 should access application scoped value whenever is required. Thanx

Do i have any issues in cluster environment? Is my approach correct ?

Thanks!


Solution

  • If:

    1. Both applications are in a clustered environment
    2. Everything in your session attributes are serializable
    3. Your sessions are persisting some datastore shared by the cluster

    Then your session attributes should be available across all servers.