Servlet不保存变量到会话

时间:2013-11-05 18:26:53

标签: jsp session servlets

这是我缺少的一小部分,我已经准备好了其余的查询,但没有一个能够正常工作,因为当我尝试将属性保存到servlet时,它不起作用。

    // used a form in a JSP
    // retrieved the data from the session same as
    // accion=request.getParameter("accion"); no errors there
    // used to query sql server to check if exist

    if(usuario!=null){

            // i dont know why but here is the problem

            HttpSession session= request.getSession(); // tried with (false)
            Usuario user =usuario;                     // same error
            session.setAttribute("usu",user);

            //Retrieval test
            Usuario test=(Usuario)request.getAttribute("usu");

            if(test!=null){
                    out.println("User != null");
            }else{
                    out.println("User == null");
            }

            //output is "User == null" and cant figure out why

            //response.sendRedirect("./intranet/adminLogin.jsp?logged=1");
    }

如果在JSP中我检查一个会话(重定向之后),它确实没有一个

    if(request.getSession(false)==null){
            out.println("#info there is no session <br />");
    }else{
            out.println("#info there is a session <br />");
    }
    // 

我需要对此进行排序,以便我无法修复所有需要会话的内容。

1 个答案:

答案 0 :(得分:3)

您的检索测试代码是从请求获取属性。这是与会话不同的对象。

替换:

    Usuario test=(Usuario)request.getAttribute("usu");

使用:

    Usuario test=(Usuario)session.getAttribute("usu");