重定向验证 [英] Validation on Redirect

查看:88
本文介绍了重定向验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

validation.java

validation.java

     try
    {
        conn = dsEvent.getConnection();
        String userCheck = "select * from customer";
        stmt = conn.createStatement();
        rs = stmt.executeQuery(userCheck);
        while(rs.next()){
           if((email.equals(rs.getString("email")))&&(password.equals(rs.getString("password"))))
           {
               RequestDispatcher rd = req.getRequestDispatcher("/success.jsp");
               rd.forward(req,res);
           }
           else
           {
              req.getSession().setAttribute("error", "The email or password you entered is incorrect. Please try again");
            res.sendRedirect(this.getServletContext().getContextPath() + "/index.jsp");
           }
        }
    } catch (SQLException ex) {
        System.err.println(ex);
    }
}

index.jsp

index.jsp

  <body>
    <h2>System</h2>
    <p style ="color:red"><%=session.getAttribute("error")!=null ? "":session.getAttribute("error") %></p>
    <form method ="post" action="validation">
        Please Login: <br/>
        Email: <input type="email" name="email" required/><br/>
        Password: <input type="password" name="password" required/><br/>
        <input type ="submit" value="Login"/>
    </form>
</body>

我正在验证电子邮件和密码。因此,当用户输入正确的电子邮件和密码时,它将转发到success.jsp。如果用户输入错误的细节,它会将页面重定向到index.jsp以及错误。

I'm doing a validation for email and password. So when user input correct email and password it will forward to success.jsp. And if the user input wrong details, it will redirect the page to index.jsp together with the error.

但我似乎无法让它在错误上工作详细信息parts.It给了我

But I can't seem to get it work on the wrong detail parts.It gave me


警告:StandardWrapperValve [com.events.ValidationServlet]:PWC1406:
Servlet.service() for servlet com.events.ValidationServlet抛出
异常

WARNING: StandardWrapperValve[com.events.ValidationServlet]: PWC1406: Servlet.service() for servlet com.events.ValidationServlet threw exception


推荐答案

为什么要迭代通过整个结果集并验证用户是否存在而不是尝试更改选择查询以检查特定用户是否存在在您的数据库中或不。

Why are you iterating through whole resultset and validating if user is present or not instead try changing your select query to check if particular user is present in your database or not.

为此,请尝试以下代码

String email = request.getParameter("email");
String password = request.getParameter("password");
String userCheck = "select * from tableName where username = ? AND password = ?";
PreparedStatement ps = con.prepareStatement(userCheck);
ps.setString(1, email);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();

而不仅仅是检查 resultset 是否为空或者没有。

And than just check whether resultset is empty or not.

检查结果集是否为空或未放在代码下面:

To check resultset is empty or not put below code:

if(rs.isBeforeFirst()) {
    request.getSession().setAttribute("email",email);
    response.sendRedirect("success.jsp");
} else {
    request.setAttribute("error", "Invalid Username & Password");
    request.getRequestDispatcher("index.jsp").forward(request, response);
}

对于 isBeforeFirst()方法查看此处

同时更改 index.jsp 以显示错误消息

Also change index.jsp to show error message

<p style ="color:red"><%=request.getAttribute("error")!=null ? request.getAttribute("error"): "" %></p>

这篇关于重定向验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆