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

We want to split the working application into two different .war files so that we can update one application without affecting the other. Each webapp will have a different user interface, different users, and a different deployment schedule.

The easiest way, apparently, is to share the same session, so if application A is installed by session.setAttribute("foo", "bar") application B, it will be able to see it.

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

Our application runs on dedicated Tomcat 5.5, there are no other applications running on the same tomcat instance, so any security issues associated with a shared session are not a problem. We are launching several instances of Tomcat, but the balancer uses sticky sessions.

If this is not possible or this exchange session is a really bad idea, leave a comment.

+51
java java-ee tomcat session
Mar 20 '09 at 12:32
source share
10 answers

You must not share HttpSession; but you can share other objects. For example, you can register an object through JNDI and access the same object in all of your applications (databases use this to join connections).

+26
Mar 20 '09 at 13:11
source share

One thing to be aware of is that the two web applications will use different class loaders. If you want to exchange objects, they must use the same version of the class from the same class loader (otherwise you will get LinkageErrors). This means that they put them in a class loader common to both web applications (for example, for a system class) or through serialization to effectively dump and restore an object in the correct class loader with the correct version of the class.

+22
Mar 22 '09 at 2:54
source share

If you want to use Spring, there is a project called Spring Session : https://github.com/spring-projects/spring-session.

Quote: "HttpSession - allows you to replace HttpSession with the neutral way of the application container (i.e. Tomcat)"

+7
Jan 15 '15 at 11:19
source share

If web applications are so closely related to each other that they need to exchange objects, why do you break them into two? Even if you manage them somewhat independently, any decent build management system should be able to create one WAR file for deployment.

A solution like Aaron with JNDI will work, but only if both web applications are running on the same server. If the devices are tightly coupled, and you will still run them on the same server ... maybe you have one WAR

If you really want them to stand independently, I would seriously study the data exchange between them. Ideally, you would like them to share only relevant data with each other. This data can be transferred back and forth via POST (or GET, if necessary), you can even consider using cookies.

+5
Mar 20 '09 at 14:23
source share

One way to do this is described in this blog post: Session Sharing in Apache Tomcat

Summary: Add emptySessionPath to connector configuration and crossContext in context

+3
Oct 17 '11 at 3:26 a.m.
source share

For Tomcat 8, I use the following configuration to share a session between two web applications:

conf / context.xml

 <Context sessionCookiePath="/"> <Valve className="org.apache.catalina.valves.PersistentValve"/> <Manager className="org.apache.catalina.session.PersistentManager"> <Store className="org.apache.catalina.session.FileStore" directory="${catalina.base}/temp/sessions"/> </Manager> ... </Context> 

I deployed the same simple web application twice log.war and log2.war :

 /log /log2 

Now I can go into /log and map the user to /log2 , this does not work with the tomcat configuration by default.

enter image description here

The session value is set and read:

 HttpSession session=request.getSession(); session.setAttribute("name",name); HttpSession session=request.getSession(false); String name=(String)session.getAttribute("name"); 

I used this project as an example: https://www.javatpoint.com/servlet-http-session-login-and-logout-example

Most examples / solutions use a database in memory, which requires more configuration work:

+1
Apr 25 '19 at 5:55
source share

You can do this by taking a servlet context using your context root.

To get a variable.

 request.getSession().getServletContext().getContext("/{applicationContextRoot}").getAttribute(variableName) 

To set a variable:

 request.getSession().getServletContext().getContext("/{applicationContextRoot}").setAttribute(variableName,variableValue) 

Note. Both applications must be deployed on the same server.

Pls let me know if you find any problem

0
Apr 30 '13 at 2:31 on
source share

Tomcat 8: I had to do: <Context crossContext="true" sessionCookiePath="/"> in conf / context.xml

more about configuration attributes here

and then set the value (e.g. @Qazi answer):

 ServletContext servletContext =request.getSession().getServletContext().getContext("contextPath") servletContext.setAttribute(variableName,variableValue) 

to get the value:

 ServletContext servletContext =request.getSession().getServletContext().getContext("contextPath") servletContext.getAttribute("user"); 
0
Jun 27 '17 at 2:25
source share

I developed a session state server for Tomcat using Python.

In this regard, I do not need to change the code already written to create / access and destroy the session. There is also a separate server / service that processes and stores the session, so a master cluster is not required. In this case, there is no session replication (as in tomcat clustering), rather it is a sharing session between web farming.

0
Jun 04 '18 at 9:44
source share

You do not have to break the application in such a way as to have high availability. You can deploy the entire application in many instances of tomcat.

-one
Mar 22 '09 at 3:44
source share



All Articles