登录_实现Servlet、登录功能_实现Service&Dao

登录_实现Servlet

前台代码

后台代码

loginServlet

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取用户名称和密码
Map<String, String[]> map = req.getParameterMap();
// 封装User对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
// 调用Service查询
UserServlce servlce = new UserServlceImpl();
User u= servlce.login(user);
ResultInfo info = new ResultInfo();
// 判断用户对象是后为null
if (u==null){
// 用户名和密码错误
info.setFlag(false);
info.setErrorMsg("用户密码错误");
}
// 判断用户是否激活
if (u!=null&&!"Y".equals(u.getStatus())){
// 用户尚未激活
info.setFlag(false);
info.setErrorMsg("您尚未激活,请激活..");
}
// 判断登录成功
if(u!=null&&"Y".equals(u.getStatus())){
info.setFlag(true);
}
// 响应数据
ObjectMapper mapper = new ObjectMapper();

resp.setContentType("application/json;charset=utf-8");
mapper.writeValue(resp.getOutputStream(),info);

}
}

 


登录功能_实现Service&Dao

UserService 


User login(User user);


@Override
public User login(User user) {
return userDao.findByUsernameAndPassoword(user.getUsername(),user.getPassword());
}
}

 

UserDao

 

//   根据用户名和密码查询的方法
User findByUsernameAndPassoword(String username, String password);
}

@Override
public User findByUsernameAndPassoword(String username, String password) {
User user =null;
try {
String sql = "select * from tab_user where username = ? and password = ?";
user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),username,password);
} catch (DataAccessException e) {
e.printStackTrace();
}
return user;
}

 

posted @ 2022-08-26 11:38  一位程序袁  阅读(42)  评论(0编辑  收藏  举报