java - Login based application which produces IllegalStateException -
the thought behind i'll redirecting page if login(correct user+pass) exsists in database , upon authentication, can store message in text file.the code pretty straightforward though i'm not sure why i'm getting illegalstateexception
this my- logininfo.java
public class logininfo extends httpservlet { private static final long serialversionuid = 1l; //i've hidden unused code protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string user= request.getparameter("user"); string pass= request.getparameter("pass"); response.setcontenttype("text/html"); printwriter pw = response.getwriter(); connection con = null; statement stmt = null; resultset rs = null; seek { class.forname("com.mysql.jdbc.driver"); con=drivermanager.getconnection("jdbc:mysql://localhost:3306/mayurtest","root","root"); string sql = "select * login user=? , pass=?"; //thanks braj stmt = con.preparestatement(sql); stmt.setstring(1, user); stmt.setstring(2, pass); rs = stmt.executequery(); while(rs.next()) { if((rs.getobject(1).tostring().equals(user))&&(rs.getobject(2).tostring().equals(pass))) //edited code { pw.println("correct login"); response.sendredirect("message.html"); //request.getrequestdispatcher("message.html").include(request,response); should write instead string message=request.getparameter("message"); //error here???? /*seems problem null values printed*/ testmessage(message); break; } else { pw.println("please check username/password again.<br>"); response.sendredirect("userlogininfo.html");//request.getrequestdispatcher("userlogininfo.html").include(request,response); should write instead break; } } } } grab (sqlexception e) { pw.println(e.getnextexception()); } grab (classnotfoundexception e) { pw.println(e.getexception()); } { seek { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (con != null) { con.close(); con = null; } } grab (exception e) { pw.close(); } } } public void testmessage(connection con,statement stmt,resultset rs,string message) throws filenotfoundexception { file file = new file("c:/users/thammaiahm/desktop/mayur desktop files/mayur.txt"); filereader fr = new filereader(file); try{ printwriter pw1 = new printwriter(new fileoutputstream("c:/users/thammaiahm/desktop/mayur desktop files/mayur.txt",true)); pw1.println( message ); pw1.close(); fr.close(); } catch(ioexception ioio){//do } } } this followed 2 html pages- userloginpage.html
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head>enter login details<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"></head> <body> <form method="post" action="http://localhost:8888/servlet/logininfo"> login :<input type="text" name="user"/><br> password:<input type="password" name="pass"/><br> <input type="submit" value="login"/> </form></body> </html> and message.html->>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head>welcome user: <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"></head> <body> <form method="post" action="http://localhost:8888/servlet/logininfo"> please come in secret message:<input type="text" name="message"/><br> <input type="submit" value="login"/> </form></body> </html> can help me find error? , maybe help me out on programming practices should follow?
here finish exception message:
java.lang.illegalstateexception: cannot phone call sendredirect() after response has been committed
it's clear exception problem.
when sendredirect() called means html response generated redirected html/jsp/servlet before doing response has sent client can't reverted.
if want include response of html/jsp/servlet utilize forward or include instead of sendredirect.
for e.g
request.getrequestdispatcher("message.html").include(request,response); there no need fetch records table. can fetch single record based on username , password using preparedstatement
sample code:
string sql = "select * login username=? , password=?"; stmt = con.preparestatement(sql); // bind values parameters. stmt.setstring(1, user); // set username stmt.setstring(2, pass); // set password rs = stmt.executequery(); if (rs.next()) { pw.println("correct login"); request.getrequestdispatcher("message.html").include(request, response); ... } else { pw.println("please check username/password again.<br>"); request.getrequestdispatcher("userloginpage.html").include(request, response); } note: don't load driver class every time need new connection. create separate class connection management.
i have posted nice connectionutil class manage connections in single class whole application.
java html eclipse servlets
No comments:
Post a Comment