在UserDao类中写一个登录验证的checkUser方法
public boolean checkUser(UserVO user) throws Exception {
Connection conn = DBUtil.connectDB();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT username FROM user_info WHERE username=? and password=? ";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setInt(2,user.getPassword());
rs = pstmt.executeQuery();//执行查询
while(rs.next()){
return true;
}
return false;
}
Servlet中的doPost方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("doPost");
String username=request.getParameter("username");
String password=request.getParameter("password");
String email=request.getParameter("email");
String sex=request.getParameter("sex");
// int ag=Integer.parseInt(age);
// int se=Integer.parseInt(sex);
UserDao dao = new UserDao();
UserVO user = new UserVO();
// Calendar can = Calendar.getInstance();
// can.set(2020, 10 - 1, 10);
// Date date = cal.getTime();
// byte[] temp=username.getBytes("iso-8859-1");
// username=new String(temp);
user.setUserName(username);
user.setPassword(password.hashCode());
user.setEmail(email);
user.setSex(sex);
try {
dao.addUser(user);
} catch (Exception e) {
e.printStackTrace();
}
request.getRequestDispatcher("/personal.jsp").forward(request, response);
System.out.println(username);
}