Search code examples
javagoogle-app-enginesessionjakarta-eehttpsession

What extra do I need to do while using HttpSession with GAE?


I read , to set a session attribute I need to put :

<sessions-enabled>true</sessions-enabled>

inside appengine-web.xml and also implement java.io.Serializable. (though I don't understand the reason for this !)

Following is one of the servlet that uses HttpSession :

 @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    String userName = request.getParameter("username");
    String password = request.getParameter("password");
    ArrayList list = new ArrayList();
    if(userName.compareTo("user") != 0 ) {
        list.add("Wrong Username");
    } else if(password.compareTo("password") != 0) {
        list.add("Wrong Password");
    }

    if(list.isEmpty()) {
        HttpSession session = request.getSession();
        if(session.isNew()) {
            session.setAttribute("UserRole", "PW :Admin");
            session.setMaxInactiveInterval(900);
            RequestDispatcher rd = request.getRequestDispatcher("abc/cpanel/PcPanel.jsp");
            request.setAttribute("SessionStatus", "JC"); // Just Created 
            rd.forward(request, response);
        }
    } else {
        response.sendRedirect("abc/cpanel/PcPanel_Login.jsp");
    }
}

After validating the username and password and entering the if block,request should be forwarded to PcPanel Login.jsp but it doesn't happen.Intead a blank page appears with the address of this servlet. But if I remove/comment all the session junk,it works fine. Why does it happen ? Am I missing something or I making a mistake somewhere ?


Solution

  • Don't use isNew(). isNew() is only true when session is first established = when user first lands on any of your pages.

    See the answer to this question: Session is NOT working - GAE/J