[英]Username and password validation from database to jsp view page
If the username and password retrieved from database are incorrect then I would like to show error on jsp page itself instead of re-directing to another page.如果从数据库中检索到的用户名和密码不正确,那么我想在 jsp 页面本身上显示错误,而不是重定向到另一个页面。
Right now I am showing a message from the validation servlet if the username and passwords are invalid.现在,如果用户名和密码无效,我将显示来自验证 servlet 的消息。 How do I show a message on front end using javascript or anyother tool to jsp view?如何使用 javascript 或任何其他工具在前端显示消息以进行 jsp 查看?
Below is my Login form:以下是我的登录表单:
<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>
And my Validate login servlet:还有我的验证登录 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();
}
}
You can use a <span>
element where you show your error message that you get from your servlet request
, here's the JSP page:您可以使用<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>
And in your servlet you set an error message in your else statment like this:在您的 servlet 中,您在 else 语句中设置了一条错误消息,如下所示:
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);
}
And to prevent showing the same error the next login in your if block where the login is successful you can reset the ErrMsg
like this:为了防止在登录成功的 if 块中下次登录时显示相同的错误,您可以像这样重置ErrMsg
:
request.setAttribute("errMsg", "");
in your else part of you Validate login servlet put this code :在您的其他部分验证登录 servlet 中放置以下代码:
if(result.next()){
response.sendRedirect("LoginSuccessful.jsp");
}
else{
HttpSession session = request.getSession();
session.setAttribute("wrong_uname_pass", "1");
response.sendRedirect("index.jsp");
}
And put below code at very first part of your index.jsp(or where is your login form)并将下面的代码放在你的 index.jsp 的第一部分(或者你的登录表单在哪里)
<%
if(session.getAttribute("wrong_uname_pass") != null){
%>
<script>
alert("wrong user name or password");
</script>
<%
session.removeAttribute("wrong_uname_pass");
}
%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.