java - Validation on Redirect -
validation.java
try { conn = dsevent.getconnection(); string usercheck = "select * 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 entered 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>
i'm doing validation email , password. when user input correct email , password forward success.jsp. , if user input wrong details, redirect page index.jsp error.
but can't seem work on wrong detail parts.it gave me
warning: standardwrappervalve[com.events.validationservlet]: pwc1406: servlet.service() servlet com.events.validationservlet threw exception
seems resultset
contains @ least 2 rows, server executing forward and/or redirect or combination of both @ least 2 times, not allowed since you're trying write response when it's closed.
solution: move validation code outside loop. make sure call requestdispatcher#forward
or httpservletresponse#redirect
occurs once in method in servlet.
also, don't recommend using redirect in case, because generates new request/response cycle attributes stored in request won't pass new request/response.
Comments
Post a Comment