Java 从数据库到jsp视图页面的用户名和密码验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30760421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:08:00  来源:igfitidea点击:

Username and password validation from database to jsp view page

javajavascriptjspservlets

提问by kittu

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. How do I show a message on front end using javascript or anyother tool to jsp view?

现在,如果用户名和密码无效,我将显示来自验证 servlet 的消息。如何使用 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();
        }   
    }

采纳答案by cн?dk

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 ErrMsglike this:

为了防止在登录成功的 if 块中下次登录时显示相同的错误,您可以ErrMsg像这样重置:

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

回答by K M PATEL

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");
}

%>