Validation on Redirect

后端 未结 3 1943
死守一世寂寞
死守一世寂寞 2021-01-17 05:28

validation.java

     try
    {
        conn = dsEvent.getConnection();
        String userCheck = \"select * from customer\";
        stmt = conn.createState         


        
3条回答
  •  暖寄归人
    2021-01-17 05:47

    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.

    To do that try below code

    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();
    

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

    For isBeforeFirst() method look here.

    Also change index.jsp to show error message

    <%=request.getAttribute("error")!=null ? request.getAttribute("error"): "" %>

提交回复
热议问题