2015-06-10 54 views
1

如果從數據庫中檢索的用戶名和密碼不正確,那麼我想在jsp頁面本身顯示錯誤,而不是重新定向到另一個頁面。從數據庫到jsp視圖頁面的用戶名和密碼驗證

現在我正在顯示來自驗證servlet的消息,如果用戶名和密碼無效。如何在前端使用javascript或其他工具在jsp視圖上顯示消息?

下面是我的登錄表單:

<form id="loginform" class="form-horizontal" name="myForm" method="POST" action="ValidateLoginServlet2.do" onSubmit="return validateLogin()"> 
    <input type="text" class="form-control" name="uname" placeholder="username">           
    <input id="login-password" type="password" class="form-control" name="pwd" placeholder="password"> 
    <input type="submit" value="Login" href="#" class="btn btn-success" /> 
</form> 

而我的驗證的登錄的​​servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
//  processRequest(request, response); 
     PrintWriter out = response.getWriter(); 

     String username = request.getParameter("uname"); 
     String password = request.getParameter("pwd"); 
     System.out.println(username); 
     System.out.println(password); 

     try 
     { 
      Connection con = OracleDBConnection.getConnection(); 
      PreparedStatement statement = con.prepareStatement("select firstname, password from registration where firstname =? and password=?"); 
      statement.setString(1, username); 
      statement.setString(2, password); 
      ResultSet result = statement.executeQuery(); 
      if(result.next()){ 
       response.sendRedirect("LoginSuccessful.jsp"); 
      }else{ 
       out.println("username and password are incorrect"); 
      } 
     }catch(Exception e){ 
      System.out.println("DB related Error"); 
      e.printStackTrace(); 
     } 
    } 
+1

我真的希望你加密這些密碼,他們得到存儲在數據庫中或針對比較值DB之前... –

+0

他們是否有加密?如果是這樣,那麼怎麼樣?請給我一個提示或鏈接。謝謝 – kittu

回答

1

您可以使用<span>元素,你表明你從你的servlet request讓你的錯誤信息,這裏的JSP頁面:

<form id="loginform" class="form-horizontal" name="myForm" method="POST" action="ValidateLoginServlet2.do" onSubmit="return validateLogin()"> 
    <input type="text" class="form-control" name="uname" placeholder="username">           
    <input id="login-password" type="password" class="form-control" name="pwd" placeholder="password"> 
    <input type="submit" value="Login" href="#" class="btn btn-success" /> 
    <span style="color:red;">${errMsg}</span> 
</form> 

而且在你的servlet設置在其他statment的錯誤信息像這樣:

if(result.next()) { 
    response.sendRedirect("LoginSuccessful.jsp"); 
}else{ 
    request.setAttribute("errMsg", "username and password are incorrect"); 
    // The following will keep you in the login page 
    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp"); 
    rd.forward(request, response); 
} 

此外,要防止出同樣的錯誤下次登錄你如果塊在登錄成功,你可以重置ErrMsg升IKE在此:

request.setAttribute("errMsg", ""); 
+0

其他部分正如您所提到的那樣被重定向到登錄頁面,但是' '錯誤信息沒有被顯示在查看頁面 – kittu

+1

@kittu那麼,只需''{errMsg}'不需要''這樣的: ' $ {errMsg} ' –

0

在你的其他部分驗證登錄的servlet把這個代碼:

if(result.next()){ 
    response.sendRedirect("LoginSuccessful.jsp"); 
} 
else{ 
    HttpSession session = request.getSession(); 
    session.setAttribute("wrong_uname_pass", "1"); 
    response.sendRedirect("index.jsp"); 
} 

並把下面的代碼放在你的index.jsp的第一部分(或者你的登錄表單在哪裏)

<% 
if(session.getAttribute("wrong_uname_pass") != null){ 
%> 
<script> 

alert("wrong user name or password"); 
</script> 

<% 
session.removeAttribute("wrong_uname_pass"); 
} 

%> 
+0

如果在客戶端瀏覽器中禁用了cookie,該怎麼辦? – kittu

+0

我試着按照你說的和它的不工作:/第二部分代碼去哪裏?它必須在腳本標記正確嗎? – kittu

+0

@kittu是它必須在腳本標記中,請現在檢查答案 –