【问题标题】:What extra do I need to do while using HttpSession with GAE?将 HttpSession 与 GAE 一起使用时,我还需要做什么?
【发布时间】:2012-08-15 02:23:35
【问题描述】:

我读了,设置我需要放置的会话属性:

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

appengine-web.xml 中并实现java.io.Serializable. (虽然我不明白这是为什么!)

以下是使用 HttpSession 的 servlet 之一:

 @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");
    }
}

验证用户名和密码并输入if 块后,请求应该被转发到PcPanel Login.jsp 但它没有发生。Inteaad 出现一个空白页面,其中包含此地址小服务程序。但是,如果我删除/评论所有会话垃圾,它工作正常。为什么会这样?我是遗漏了什么还是我在某处犯了错误?

【问题讨论】:

    标签: java google-app-engine session jakarta-ee httpsession


    【解决方案1】:

    不要使用isNew()isNew() 仅在首次建立会话 = 用户首次登陆您的任何页面时为真。

    查看此问题的答案:Session is NOT working - GAE/J

    【讨论】:

    • 在一种情况下,我只想要刚刚验证凭据的用户,请参阅页面。我应该怎么做?每当用户通过验证其凭据登录时,就会创建一个新会话。
    • 不,会话与身份验证无关。会话在用户首次访问您的任何页面时建立。关于您的问题:当用户通过身份验证(=凭据验证)时,您应该在会话中设置一些属性,即session.setAttribute("authenticated", true)
    • 如果我在用户验证凭据后检查属性 authenticated,它将正常工作。但是,如果有人直接访问该 JSP 页面,它将给我一个null pointer exception(对于未定义的属性:已验证)。而不是向访问者显示服务器的错误消息,我想将他重定向到登录页面。我应该如何处理这个?
    • 另外,您可以在 JSP 页面中进行重定向:exampledepot.com/egs/javax.servlet.jsp/redirect.html
    • jsp中的代码:&lt;% if( (Boolean)session.getAttribute("authenticated") ) { %&gt; ....html and other jsp..then at the end.. &lt;% } else { response.sendRedirect("login.jsp") %&gt;
    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多