实际上我在这段代码中遇到了逻辑问题
问题:虽然我输入了正确的 emp_id 和密码,但 if 部分运行正常,但只要我尝试运行此页面(运行方式->在 Server-Tomcat 服务器上运行),就在此 if 条件下......它将进入 else 条件——这使我无法在 if 子句中运行 jsp 页面。
else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
当它满足 if 子句时,我也想运行 jsp 页面。
这是我的代码:
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();
}
我哪里出错了……在这个编码环境中……//