《黑马旅游网》综合案例(五)登录功能
登录功能
分析:
Servlet层:
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码的校验 String check = request.getParameter("check"); //从session中获取验证码 HttpSession session = request.getSession(); String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER");//保证验证码只使用一次 //比较 if (checkcode_server==null || !checkcode_server.equalsIgnoreCase(check)){ //验证码错误 ResultInfo info = new ResultInfo(); //注册失败 info.setFlag(false); info.setErrorMsg("验证码错误!"); //将info对象序列化json并且写回客户端 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //获取用户名和密码数据 Map<String, String[]> map = request.getParameterMap(); //封装user对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //调用Service查询 UserServiceImpl service = new UserServiceImpl(); User u = service.login(user); request.getSession().setAttribute("user",u) 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(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),info); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
Service和Dao层:
UserService:添加一个方法
User login(User user);
UserServiceImpl:
/** * 登录方法 * @param user * @return */ @Override public User login(User user) { return userDao.findByUsernameAndPassword(user.getUsername(),user.getPassword()); }
UserDao:添加一个方法
User findByUsernameAndPassword(String username, String password);
UserDaoImpl:
/** * 根据用户名和密码查询方法 * @param username * @param password * @return */ @Override public User findByUsernameAndPassword(String username, String password) { User user = null; try { //定义sql String sql = "select * from tab_user where username = ? and password=?"; //执行sql user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username,password); } catch (DataAccessException e) { } return user; }
前台 login 页面:
给登录按钮添加一个id
<script> $(function () { //给登录按钮绑定单击事件 $("#btn_sub").click(function () { //发送ajax请求,提交表单数据 $.post("loginServlet",$("#loginForm").serialize(),function (data) { if (data.flag){ //登录成功 location.href = "index.html"; }else { //登录失败 $("#errorMsg").html(data.errorMsg); } }); }); }); </script>
用户姓名提示信息功能:
header.html:
将span内容删除添加一个id
<!-- 头部 start --> <script> $(function () { $.get("findUserServlet",{},function (data) { var msg = "欢迎回来,"+data.name; $("#span_username").html(msg); }); }); </script>
FindUserServlet:
@WebServlet("/findUserServlet") public class FindUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session中获取登录用户 Object user = request.getSession().getAttribute("user"); //将user写回客户端 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),user); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
进行登录(如果还是admin可以试着把缓存进行清理)
退出登录
访问Servlet,将session销毁
跳转到登录页面
header.html:在header页面中编写退出
ExitServlet:
@WebServlet("/exitServlet") public class ExitServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //销毁session request.getSession().invalidate(); //跳转登录页面 response.sendRedirect(request.getContextPath()+"/login.html"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
点击退出欢迎......就没有了