GAE/J Session is different from J2EE Session. I am confused with GAE/J session, it have to be a little implementation to use. The following is what I have found …
- By default, you are only allowed read access to the Session in GAE. If you want to write to the session, you have to enable by adding to your WEB-INF/appengine-web.xml.
<sessions-enabled>true</sessions-enabled>
- Next, all the objects that you are going to persist to the session must implement the java.io.Serializable interface because App Engine stores session data in the datastore and memcache.
This code is my problem. I persist List() to the session but List is not value of object implement the Serializable. So, I fixed later.
List<Category> catalogs=service.findAllCatalog();
request.getSession(true).setAttribute("catalogs", catalogs);
So, any objects you put in the session must be Serializable, the value can persist.
- It need to invoke setAttribute() again after modified value from getAttribute(). Every setAttribute will trigger a new serialization and write to the datastore.This third step solved my question why session data is remained in App Engine though data was clear. Here is my code..
ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("cart");
cart.clear(); // cart is empty in Standard JEE environment but not in GAE
So, I added this…
request.getSession().setAttribute("cart", cart); // need to invoke setAttribute() again in GAE
After made three steps, GAE/J session work well. Please leave a comment if you have any addition or correction.
Reference:
http://blog.stringbuffer.com/2009/04/http-sessions-and-google-app-enginej.html
http://blog.afewguyscoding.com/2011/02/httpsession-google-app-engine
http://blog.comtaste.com/2010/07/managing_session_data_in_gae_a.html