mxlse
mxlse

Reputation: 2784

Use same session with two tomcat applications

I have two applications located on the same tomcat7 server:

  1. https://my.application.com/writeApplication
  2. https://my.application.com/readApplication

Now what I want to achive is that the writeApplication writes something into the session, afterwards the readApplication gets started and reads this information out of the session.

In both web.xml files I inserted:

<session-config>
    <session-timeout>720</session-timeout>
    <cookie-config>
        <name>JSESSIONID</name>
        <path>/</path>
        <http-only>true</http-only>
    </cookie-config>
</session-config>

In the writeApplication (a simple servlet) I write into the session like:

HttpSession session = req.getSession();
session.setAttribute("test", "myString");

Like wished the JSESSIONID-Cookie is written to "/" so https://my.application.com/.

In my readApplication (a GWT application) I want to read this information out of the session by using:

HttpSession httpSession = getThreadLocalRequest().getSession();
String myString = (String) httpSession.getAttribute("test");

In this case I always receive null.

If I write something by the readApplication the path is the same like in the writeApplication, but if I want to read I can't get the information I was adding with the servlet.

Shouldn't it work like this?

Upvotes: 2

Views: 1702

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

This generally cannot be done, as this SO question discusses.

However, one common alternative to using the session, which is even viewed as being preferable to using the session, is writing to a database. It is generally possible for two different Java web applications to share the same database, even the same table. You could have your two webapps write and read shared state from a common database.

Upvotes: 2

Related Questions