重定向验证

时间:2015-06-13 17:39:32

标签: java jsp servlets

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

  <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以及错误。

但我似乎无法在错误的细节部分上工作。它给了我

  

警告:StandardWrapperValve [com.events.ValidationServlet]:PWC1406:   servlet com.events.ValidationServlet的Servlet.service()抛出   例外

3 个答案:

答案 0 :(得分:0)

好像ResultSet包含至少两行,所以服务器正在执行转发和/或重定向或两者的组合至少两次,这是不允许的,因为您正在尝试在回复已经结束时写下回复。

解决方案:将验证码移到任何循环之外。确保仅在您的servlet方法中对RequestDispatcher#forwardHttpServletResponse#redirect的调用一次

此外,在这种情况下,我不建议使用重定向,因为它会生成新的请求/响应周期,因此请求中存储的所有属性都不会传递给此新请求/响应。

答案 1 :(得分:0)

为什么要遍历整个resultset并验证用户是否存在而是尝试更改select查询以检查数据库中是否存在特定用户。

为此,请尝试下面的代码

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是否为空。

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

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()方法查看here

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

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

答案 2 :(得分:0)

这主要是对Luiggi Mendoza的回答。 您不能在循环中使用forwardsendRedirect,除非您在循环中只传递一次。正如您所知,servlet会抛出导致错误的异常。

您可以将这些代码移到循环之外:

    boolean found = false;
    while( (! found) && rs.next()){
       if((email.equals(rs.getString("email")))&&(password.equals(rs.getString("password"))))
       {
           found = true;
       }
    }
    if (found) {
       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");
    }

但是,你可以在结果集上循环手动,当你可以让数据库找到匹配时:

    conn = dsEvent.getConnection();
    String userCheck = "select count(*) from customer where email = ? and password = ?";
    stmt = conn.prepareStatement();
    stmt.setString(1, email);
    stmt.setString(2, password);
    rs = stmt.executeQuery(userCheck);
    if (rs.next().getInt(1) > 0) 
       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");
    }