2016-09-27 5 views
0

ここに私のLoginServletクラスのコードです。jspのif文を使用してこのエラーが発生するのはなぜですか?

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    String userName = request.getParameter("username"); 
    String password = request.getParameter("password"); 

    boolean loginFailed; 

    User user = null; 

    user = loginService.getUser(userName); 

    if(user != null && user.getPassword().equals(password)){ 

     userType = loginService.getUserType(user).getUser_type(); 

     if(userType.equals(USER_TYPE_CUSTOMER)){ 
      HttpSession session = request.getSession(); 
      session.setAttribute("username", user.getUsername()); 
      response.sendRedirect("home_customer.jsp"); 
     }else{ 

     } 
    }else{ 
     loginFailed = true; 

     request.setAttribute("username", userName); 
     request.setAttribute("loginFailed", loginFailed); 
     RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp"); 
     dispatcher.forward(request, response); 
    } 
} 

私はloginFailedという名前のブール値を持っています。ユーザーがアカウントを持っていない場合は、値がtrueになります。私はこの変数をチェックするためにjspページにコードを作った。ここに私のJSPコードです。

<body> 
<div class="container container-table" id="login"> 
    <div class="row vertical-center-row"> 
     <div class="col-md-4 col-md-offset-4"> 
      <div style="background-color: #297f56; padding: 15px; border-radius: 5%;"> 
       <h1 style="text-align: center; color: white;">JJ STORE</h1> 
       <% Boolean loginFailed = (Boolean) request.getAttribute("loginFailed"); 
        if(loginFailed){ 
         out.println("<div class='alert alert-warning'>Warning! Dont submit this.</div>"); 
        }%> 
       <form action="processLogin" method="post"> 
       <div class="form-group" > 
        <label for="email" style="color: white;">Username:</label> 
        <input type="text" class="form-control" name="username" value="${username}"> 
       </div> 
       <div class="form-group"> 
        <label for="pwd" style="color: white;">Password:</label> 
        <input type="password" class="form-control" name="password"> 
       </div> 
       <button type="submit" class="btn btn-default">Submit</button> 
       </form> 
      </div> 
     </div> 
    </div> 
</div> 

私はこの問題はここにあるのかわからないです。 ifブロックの条件を "true"に変更してコードをスムーズに行ってみました。しかし、サーブレットから取得したブール値を置くと、このエラーが発生します。

enter image description here

私が間違って何をしているのですか?

答えて

2

エラーが発生した場合にのみ、loginFailedを設定しています。それが成功すると、属性を設定していないので、JSPでヌル値が得られ、if(loginFailed)を呼び出すとNullPointerExceptionになります。

問題を解決する1つの方法は、アンボクシングなしBooleanオブジェクトをテストすることです:

if (Boolean.TRUE.equals(loginFailed)) 

別のオプションは、それが成功したか失敗したかの属性を設定することを確認することです。

if (user != null && user.getPassword().equals(password)) { 
    //... 
    loginFailed = false; 
} else { 
    //... 
    loginFailed = true; 
} 
request.setAttribute("loginFailed", loginFailed); 
+0

あなたの最初の提案は私の問題を解決しました!私の初めてのことです。私は研修生のソフトウェアエンジニアです。私に与えられたプロジェクトの1つは、ウェブサイトを(サーブレットとJSPを使用して)基本的な方法で作成することでした。私はブール値の代わりにブール値を使用しなければならない理由を理解していません(私はこれを別の答えから得ています)。とにかく、ありがとう! – saluyotamazing

関連する問題