11 UserService中添加login()重载方法 参数为user,LoginServlet使用新的login()

1.UserService中添加login()重载方法 参数为user

public User login(User user) throws Exception {
		// TODO Auto-generated method stub
		User u = userDao.findUser(user.getUsername(), user.getPassword());
		if (u != null) {
			return u;
		} else {
			throw new Exception("用户名和密码不正确");
		}
}

 

2.LoginServlet使用新的login(User user)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		IUserService userService=new UserServiceImpl();
		User user=new User();
		try {
			ConvertUtils.register(new DateLocaleConverter(), Date.class);
			
			BeanUtils.populate(user, request.getParameterMap());
			System.out.println("请求参数"+user);
			userService.register(user);
			response.getWriter().write("注册成功,3秒后转回主页");
			response.setHeader("refresh", "3;url="+request.getContextPath()+"/index.jsp");
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.getWriter().write("注册失败,"+e.getMessage());
		}		
}

 

 

 

你可能感兴趣的:(javaweb,网上书城)