Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to share session state between different applications in tomcat?

We want to split a working application in two different .war files in order to be able to update one app without affecting the other. Each webapp will have different a UI, different users and different deploy schedule.

The easiest path seems to be sharing the same session, so if app A set session.setAttribute("foo", "bar") app B will be able to see it.

Is there a way to share the HttpSession state for both apps in the same Tomcat instance?

Our app is running on a dedicated Tomcat 5.5, there are no other apps running on the same tomcat instance, so any security concerns regarding the session sharing are not a problem. We're running multiple Tomcat instances, but the balancer is using sticky sessions.

If it's not possible or this session sharing is a really bad idea please leave a comment.

like image 835
Serxipc Avatar asked Mar 20 '09 12:03

Serxipc


People also ask

Can we run multiple applications in Tomcat?

Simply drop both war files into Tomcat's webapps folder. That is all you need to do. By default, Tomcat expands ("explodes" some say) each war (technically a zip file) into a folder and automatically deploys the app for you. This happens on the fly if Tomcat is already running, or on startup when you launch Tomcat.

How does Tomcat store session data?

Tomcat's sessions are stored according to chosen session manager. If we pick the standard manager (StandardManager class saw previously), all session data will be saved into Java heap. The storing of session data in JVM memory is a dangerous idea.

How does Tomcat manage session?

In session management, Tomcat creates a session id whenever client's first request gets to the server (However, other servlet containers may behave differently). Then it inserts this session id into a cookie with a name JSESSIONID and sends along with the response.


2 Answers

You should not share HttpSession; but you can share other objects. For example, you can register an object via JNDI and access the same object in all your apps (databases use this to pool connections).

like image 90
Aaron Digulla Avatar answered Sep 25 '22 20:09

Aaron Digulla


One thing to be aware of is that two web apps will use different classloaders. If you want to share objects, they need to use the same version of the class from the same classloader (or else you will get LinkageErrors). That means either putting them in a classloader shared by both web apps (system classpath for example) OR using serialization to effectively drain and reconstitute the object in the right classloader with the correct version of the class.

like image 37
Alex Miller Avatar answered Sep 22 '22 20:09

Alex Miller