Changing cookie JSESSIONID name

Viewed 78882

I have a requirement of having to run multiple tomcat server in single physical box. While accessing these from a browser, when user switches between the applications, it results in logging out the user previously access application. This is because of JSESSIONID cookie conflict.

One possible solution is to run each applications in different context. Unfortunately, my applications will not work in context path setting as none of the resources are accessed with request.getContextPath() prepended in front.

This leaves me to change the name of cookie JSESSIONID to resolve the conflict. Is there a way to do this? If yes, how?

Hope I'm clear in stating my question.

Note: All my application are running in different port in the same machine.

8 Answers

Not 100% sure if this will work, but you can use the jvmRoute attribute, which is generally used in a load-balanced/clustered environment for the load balancers to be able to tell the nodes apart. Example:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">

This will end up generating a JSESSIONID value that looks like "ABCDEF123456.node1".

Documentation link.

I found it in Tomcat at /tomcat/conf/server.xml

server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="instanceName">

5D33F755D8D75EF7C8E840.instanceName

    final SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
    sessionCookieConfig.setSecure(true);
    sessionCookieConfig.setHttpOnly(true);
    // Set __Host- prefix
    sessionCookieConfig.setName("__Host-JSESSIONID");
Related