2

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?

mxlse
  • 2,654
  • 17
  • 31

1 Answers1

2

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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • My hope was to avoid this. In this case I would have to give a parameter with the id of the db entry within the url. How could I be sure that only the one who called the *writeApplication* is able to read this information out of the db. – mxlse Sep 12 '16 at 14:53
  • @mxlse Playing the devil's advocate, how could Tomcat (or JBoss, etc.) manage the same, if a session is to be shared across two web applications? – Tim Biegeleisen Sep 12 '16 at 14:55
  • 1
    Alright , thank you. I think I will solve it with an own cookie in my implementation. – mxlse Sep 12 '16 at 15:24