The idea behind this is that i'll be redirecting to a page if the login(correct user+pass) exsists in my database and upon this authentication, they can store a message in a text file.The code is pretty straightforward though I'm not sure as to why I'm getting an IllegalStateException
This is my- logininfo.java
public class logininfo extends HttpServlet
{ private static final long serialVersionUID = 1L;
//I've hidden all the 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;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mayurtest","root","root");
String sql = "SELECT * FROM LOGIN where user=? and 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); is what I should write instead
String message=request.getParameter("message"); //error here???? /*seems to be a problem as now all I get are 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); is what I should write instead
break;
}
}
}
}
catch (SQLException e)
{
pw.println(e.getNextException());
}
catch (ClassNotFoundException e)
{
pw.println(e.getException());
}
finally
{
try
{
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
}
catch (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 something
}
}
}
This is now followed by the two 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 the 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 enter you secret message:<input type="text" name="message"/><br>
<input type="submit" value="login"/>
</form></body>
</html>
Can you help me find the error? And maybe help me out on any programming practices I should follow?