When I store something in the session via HttpSession
in module A:
HttpSession session = req.getSession(true);
session.setAttribute("username", "Eng.Fouad");
then I try to retrieve this information from within module B (during the same browser session):
HttpSession session = req.getSession(true);
String username = session.getAttribute("username"); // null!
I got null
as a value, which means different session.
How to share session across multiple modules in GAE?
Old Answer which I think it doesn't work well:
I've been trying to solving this issue for weeks. It turns out that the modules don't share sessions because the cookies are different across different sub-domains (module1.apphost.com cookies != module2.apphost.com cookies). To solve it, simply set cookies domain in web.xml
of each module:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<context-param>
<param-name>org.mortbay.jetty.servlet.SessionDomain</param-name>
<param-value>.apphost.com</param-value>
</context-param>
<context-param>
<param-name>org.mortbay.jetty.servlet.SessionPath</param-name>
<param-value>/</param-value>
</context-param>
<context-param>
<param-name>org.mortbay.jetty.servlet.SessionURL</param-name>
<param-value>none</param-value>
</context-param>
...
</web-app>
EXTRA:
Here is a list of all init parameters you can use with Jetty cookies:
EDIT: Workaround for development environment:
- Make the port of each module fixed (using JVM arg
-Dcom.google.appengine.devappserver_module.{module_name}.port=8081
). For example, module1 is always hosted at localhost:8888, and module2 is always hosted at localhost:8889. See this answer. - Bind the localhost with the port of each module to a custom domain using Fiddler. For example, moule1.gaelocal.com points to localhost:8888 and moule2.gaelocal.com points to localhost:8889. See this answer.
- Update
web.xml
of each module and replace.apphost.com
with.gaelocal.com
(or just use.apphost.com
for both environment).
HttpSession
attributes in bothDataStore
andMemCache
. – Curiosa