如果從數據庫中檢索的用戶名和密碼不正確,那麼我想在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();
}
}
我真的希望你加密這些密碼,他們得到存儲在數據庫中或針對比較值DB之前... –
他們是否有加密?如果是這樣,那麼怎麼樣?請給我一個提示或鏈接。謝謝 – kittu