java - Username and password validation from database to jsp view page -
if username , password retrieved database incorrect show error on jsp page instead of re-directing page.
right showing message validation servlet if username , passwords invalid. how show message on front end using javascript or anyother tool jsp view?
below 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 validate login 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 registration firstname =? , password=?"); statement.setstring(1, username); statement.setstring(2, password); resultset result = statement.executequery(); if(result.next()){ response.sendredirect("loginsuccessful.jsp"); }else{ out.println("username , password incorrect"); } }catch(exception e){ system.out.println("db related error"); e.printstacktrace(); } }
you can use <span>
element show error message servlet request
, here's jsp page:
<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 servlet set error message in else statment this:
if(result.next()) { response.sendredirect("loginsuccessful.jsp"); }else{ request.setattribute("errmsg", "username , password incorrect"); // following keep in login page requestdispatcher rd = request.getrequestdispatcher("/login.jsp"); rd.forward(request, response); }
and prevent showing same error next login in if block login successful can reset errmsg
this:
request.setattribute("errmsg", "");
Comments
Post a Comment