老师,我登陆页面的时候 输什么都是用户名和密码错误 代码看了和文档是一样的
public class UserLoginDaoImpl implements UserLoginDao {
//用户数据库登陆查询
public Users selectUsersByUsernameAndUserpwd(String username, String userpwd) {
Users users = null ;
Connection conn=null;
try {
conn = JdbcUtils.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from users where username=? and userpwd=?");
ps.setString(1,username); //绑定参数
ps.setString(2,userpwd);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
users = new Users();
users.setUsersex(resultSet.getString("usersex"));
users.setUserpwd(resultSet.getString("userpwd"));
users.setUsername(resultSet.getString("username"));
users.setUesrid(resultSet.getInt("userid"));
users.setPhonenumber(resultSet.getString("phonenumber"));
users.setQqnumber(resultSet.getNString("qqnumber"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
return users;
}
}
public class UserLoginServiceimpl implements UserLoginService {
//用户登陆业务
public Users userLogin(String username, String userpwd) {
//实例化持久业务层
UserLoginDao userLoginDao = new UserLoginDaoImpl();
Users users = userLoginDao.selectUsersByUsernameAndUserpwd(username,userpwd);
if(users == null){
throw new UserNotFoundException("用户名或密码有误"); //自定义异常
}
return users;
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String userpwd = req.getParameter("userpwd");
try {
UserLoginService userLoginService = new UserLoginServiceimpl();
Users users = userLoginService.userLogin(username,userpwd); //登陆成功返回一个user对象
//建立客户端与服务端的会话状态
HttpSession session = req.getSession();
session.setAttribute(Constans.USR_SESSION_KEY,users);
//跳转首页 ( 重定向方式 )
resp.sendRedirect("main.jsp");
}catch (UserNotFoundException e){
req.setAttribute("msg",e.getMessage());
req.getRequestDispatcher("login.jsp").forward(req,resp);
}catch (Exception e){
resp.sendRedirect("error.jsp");
}
}