Sharing session data between two contexts in tomcat 7

If you have two contexts, say /app1 and /app2 and you want to share session data between them you can do it by the following way:

  1. In context.xml set sessionCookiePath to / and set crossContext to true:
    <Context sessionCookiePath="/" crossContext="true">
  2. In both applications write this in web.xml:
    <filter>
    	<filter-name>userSessionFilter</filter-name>
    	<filter-class>myPackage.UserController</filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>userSessionFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
  3. In the first application, create a class, say UserController, which implements Filter and in doFilter method:
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	ServletContext sctx = ((HttpServletRequest) request).getSession().getServletContext();
    	String testStr = (String) sctx.getAttribute("attr");
    	if(testStr == null){
    		testStr = "test";
    		sctx.setAttribute("attr", testStr);
    	}
    	chain.doFilter(request, response);
    }
  4. In the second application, create a class, say UserController, which implements Filter and in doFilter method:
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	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);
    	chain.doFilter(request, response);
    }
  5. Now the testStr value which set in the first app’s UserController class, can be obtained by the second app’s UserController class..

Advertisements

One response to “Sharing session data between two contexts in tomcat 7

  1. Nice article but the title is wrong you are sharing application context not session. I managed to do session sharing starting with your example and using JMX. This is a tomcat (and probably JBOSS) solution , I don t know what other app server expose via JMX

    HttpSession session = ((HttpServletRequest) request).getSession(true);
    MBeanServer beanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    String opSig[] = { String.class.getName(), String.class.getName() };

    document = (String) beanServer.invoke(new ObjectName(
    “Catalina:type=Manager,context=/app1,host=localhost”), “getSessionAttribute”, new Object[] {
    session.getId(), “__document” }, opSig);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s