Actually I’m facing logical problem in this code
Issue : While I’m putting the right emp_id and password,the if portion is running properly,but within this if condition whenever I’m trying to running this page(Run as->Run on Server-Tomcat server)….it’s going to the else condition-which makes me unable run the jsp page whithin the if clause.
else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
I want to run the jsp page aswell when it satisfy the if clause.
Here is my code:
try {
String url="jdbc:mysql://localhost/app";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,"****","******");
stmt = con.prepareStatement("select * from ofc where emp_id=? and password=?");
stmt.setString(1, employee);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if(rs.next()) { //I want to print this jsp page for this If condition
System.out.println("2> Employee Id : "+employee+" && Password : "+password);
System.out.println("3> This employee "+employee+" exsists in the database and will be there");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.print("<html><body>"); // It's my JSP page,& start from here
out.print("<head>");
out.print("<title>Policy Page</title>");
out.print("</head>");
List<String> devices = Store.getDevices();
if (devices.isEmpty())
{
out.print("<h2>No One is there!</h2>");
}
else
{
out.print("<h2>" + devices.size() + " device(s) are there!</h2>");
out.print("<form name='form' method='POST' action='sendAll'>");
out.print("<input type='text' name='policy'>");
resp.setStatus(HttpServletResponse.SC_OK);
out.print("<input type='submit' value='Apply Policy'>");
out.print("</form>");
// getServletContext().getRequestDispatcher("/home").forward(req, resp);
}
out.print("</body></html>"); //Completes here
resp.setStatus(HttpServletResponse.SC_OK);
}
else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
catch(Exception e) {
e.printStackTrace();
}
Where I’m going wrong….in this coding context …//
When you debug your code, does the debugger go into the IF statement – actually enter within the inner brackets of the IF statement, or does it merely evaluate the IF statement, (evaluate at it to “IF(False)”) and then move on to the else statement?
Maybe your mySQL query doesn’t return any results? Try using the HasRows property.