@ WebServlet ("/Login ")
Public class Login extends the HttpServlet {
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
String username=request. The getParameter (" username ");
String password=request. The getParameter (" password ");
UserDao UserDao=new UserDao ();
User User= userdao. Selectuser (username, password);
If (the user!=null) {
Request. GetRequestDispatcher ("/usercenter. JSP "). The forward (request, response);
//the HttpSession session=request. GetSession ();
//session. SetAttribute (" user ", the user);
} else {
//request. SetAttribute (" MSG ", "user name or password error");
Request. GetRequestDispatcher ("/index. The JSP "). The forward (request, response);
}
}
}
This is UserDao
Public User selectuser (String username, String password) throws the Exception {
User User=new User ();
Connection con=new DButil (). The getConnection ();//call DButil getConnection () method to connect to the database
String SQL="select * from the user where the username=? And password=?" ;
PreparedStatement ps=null;
The ResultSet rs=null;
Ps=con. PrepareStatement (SQL);//request to the database by
Ps. SetString (1, username);//set the request parameter
Ps. SetString (2, password);
Rs=ps. ExecuteQuery ();//get the request of the result set
If (rs), next ()) {//judge whether there is any query result
String name=rs. Get String (" username ");//username is header username field in the database, the query result is assigned to the name
String paw=rs. Get String (" password ");
User. SetUsername (name);//call the method userset assignment
The user. The setPassword (paw);
} to return the user;
}
CodePudding user response:
Because you're userdao. Selectuser (username, password); Methods will exception thrown outward, so when you call this method, or through the try... Catch the exception handling, or continue to throw,