Hi Marton. Yes, you need to explicitly call setAttribute() when you modify
any object in the session. Otherwise, the object won't be updated in App
Engine's cache/datastore, which are the mechanisms it uses to back sessions
- Jason

On Thu, Sep 3, 2009 at 2:11 AM, Marton Papp <mapr...@gmail.com> wrote:

>
> Hi!
>
> I am using HttpSession to store some information as session
> attributes. When I am testing the application locally, it works fine.
> But when I upload it to production, it seems that any session
> attributes that I retrieve from the session and modify it are not
> persisted back to the session again, unless I specifically call
> HttpSession.setAttribute() in each request. I am not sure whether it
> is the expected behavior or it is bug. Here are the two test cases:
>
> Test1:
>
> public class Test_gae_sessionServlet extends HttpServlet {
>        private static final String SESSION_ATTRIBUTE_NAME =
> "test_session_attribute";
>
>        public void doGet(HttpServletRequest req, HttpServletResponse resp)
>                        throws IOException {
>                HttpSession session = req.getSession();
>                resp.setContentType("text/plain");
>
>                SessionContext sessionContext = (SessionContext)
> session.getAttribute
> (SESSION_ATTRIBUTE_NAME);
>
>                resp.getWriter().println("session retreived: " +
> sessionContext);
>
>                if (sessionContext==null) {
>                        sessionContext = new SessionContext();
>                        resp.getWriter().println("session created: " +
> sessionContext);
>
>                        // calling setAttribute only when the object is
> first created
>                        resp.getWriter().println("putting context into
> session: " +
> sessionContext);
>                        session.setAttribute(SESSION_ATTRIBUTE_NAME,
> sessionContext);
>                }
>
>                sessionContext.value++;
>
>                resp.getWriter().println("context at end of retquest: " +
> sessionContext);
>        }
> }
>
>
> Test2:
>
> public class Test_gae_sessionServlet extends HttpServlet {
>        private static final String SESSION_ATTRIBUTE_NAME =
> "test_session_attribute";
>
>        public void doGet(HttpServletRequest req, HttpServletResponse resp)
>                        throws IOException {
>                HttpSession session = req.getSession();
>                resp.setContentType("text/plain");
>
>                SessionContext sessionContext = (SessionContext)
> session.getAttribute
> (SESSION_ATTRIBUTE_NAME);
>
>                resp.getWriter().println("session retreived: " +
> sessionContext);
>
>                if (sessionContext==null) {
>                        sessionContext = new SessionContext();
>                        resp.getWriter().println("session created: " +
> sessionContext);
>                }
>
>                // calling setAttribute in every request
>                resp.getWriter().println("putting context into session: " +
> sessionContext);
>                session.setAttribute(SESSION_ATTRIBUTE_NAME,
> sessionContext);
>
>                sessionContext.value++;
>
>                resp.getWriter().println("context at end of retquest: " +
> sessionContext);
>        }
> }
>
>
> The object stored in the session:
>
> public class SessionContext implements Serializable {
>
>        private static final long serialVersionUID = -5151175222401820614L;
>
>        public int value = 0;
>
>        @Override
>        public String toString() {
>                return "SessionContext [value=" + value + "]";
>        }
>
> }
>
>
> In Test1 it is always the first version of the SessionContext that is
> retreived in each request, no matter that I try to change its contents
> during each request. Test2 works as expected. Is it so that I need to
> call HttpSession.setAttribute() for any object that is expected to
> change during the request? Is it according to the servlet
> specification?
>
>
> Marton
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to