Import the Java. IO. IOException;
The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. The annotation. WebServlet;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;
Import the Java. SQL. *;
Import the Java. Util. Concurrent. RunnableScheduledFuture;
/* *
* the Servlet implementation class loginserver
*/
@ WebServlet ("/loginserver ")
Public class loginserver extends the HttpServlet {
Private static final long serialVersionUID=1 l;
/* *
* the Default constructor.
*/
Public loginserver () {
//TODO Auto - generated constructor stub
}
/* *
* @ see HttpServlet# doGet (it request, HttpServletResponse response)
*/
Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
//TODO Auto - generated method stub
Response. GetWriter (). Append (" Served at: "), append (request) getContextPath ());
}
/* *
* @ see HttpServlet# doPost (it request, HttpServletResponse response)
*/
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
The response. SetContentType (" text/HTML; Charset utf-8 ");
The response. SetCharacterEncoding (" utf-8 ");
//TODO Auto - generated method stub
//doGet (request, response);
//get the user name
String name=request. The getParameter (" username ");
String password=request. The getParameter (" password ");
boolean flag=false;
//links database
The Connection Connection=null;
PreparedStatement pStatement=null;
The ResultSet rSet=null;
Try {
Class.forname (". Com. Mysql. JDBC Driver ");
The String url="JDBC: mysql://localhost: 3306/textdb";
The connection=DriverManager. GetConnection (url, "root", "123456");
//execute SQL command
String SQL="select the username and password from tb_user where username=? And password=?" ;
PStatement=connection. PrepareStatement (SQL);
PStatement. SetString (1, name);
PStatement. SetString (2, password);
RSet=pStatement. ExecuteQuery ();
If (rSet), next ()) {
Flag=true;
}
RSet. Close ();
PStatement. Close ();
Connection. The close ();
} the catch (Exception e) {
//TODO to automatically generate the catch block of
e.printStackTrace();
}
//execution run results
if(! Flag) {
The response. GetWriter (). Println (" the user name or password error ");
}
The else {
The response. SendRedirect (" https://www.baidu.com/");
}
}
}
The code is shown in figure is really hard to understand
CodePudding user response:
The breakpoint at to know, the problem is the result there has a problem in obtaining data has always been a false flag variable, so the following false invert to true output password errorCodePudding user response:
The password you are using the request received is of type String, your database password is of type int, data type does not match the complains, of course, you put your user and password data type conversion right before the