2017-04-26 23 views
0

loggedIn Stringオブジェクトをフラグとして使用しようとしています。ログに出力する際に​​null値を返すのはなぜですか?Javaセッション属性がnullを返す理由

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    String loggedIn = "false"; //create a marker to tell if the user is logged in 

    String url = "/index.jsp"; 
    HttpSession session = request.getSession(); 

    // get a user object from the session. 
    User sessionUser =(User) session.getAttribute("user"); 

    // create empty message strings 
    String messageSuccess = ""; 
    String passwordMismatch = ""; 


    session.setAttribute("loggedIn", loggedIn); 
    System.out.println("The loggedIn attribute of the session object is: " + session.getAttribute(loggedIn)); 

    String action = request.getParameter("action"); 

    if (action == null) { 
     action = "join"; 
    } 

    if (action.equals("join")) { 
     url = "/new_customer.jsp"; 

    } else if (action.equals("signIn")) { 

//  User sessionUser = (User) session.getAttribute("sessionUser"); 

     if (sessionUser == null) { 

      url = "/login_failure.jsp"; 

     } else { 

      String username = request.getParameter("username"); 
      String password = request.getParameter("password"); 

      if (username.equals(sessionUser.getUserName()) && password.equals(sessionUser.getPassword())) { 
       url = "/account_activity.jsp"; 
       loggedIn = "true"; 
       session.setAttribute("loggedIn", true); 

      } else { 
       url = "/login_failure.jsp"; 
      } 
     } 
    } 

答えて

0

String loggedIn = "false"; 

との組み合わせで

session.getAttribute(loggedIn) 

ので

session.getAttribute("false") 

に評価し、その属性は、セッションに設定されていない可能性が高いでしょう。

おそらく

session.getAttribute("loggedIn") 
を探しています
関連する問題