案例说明
我们使用原本 第11节的代码进行改进,添加用户登录到主页后显示自己名字的功能和添加注销登录的功能。
思路设计
主页获取用户名设计
- 在LoginServlet中,我们在判断用户账号密码正确后,跳转主页之前,创建session并将用户对象添加到session中,在主页获取session中的对象即可。
- 同样地,在3天免登陆的情况下,在cookieServlet中跳转主页之前,创建session并将用户对象添加到session中,在主页获取session中的对象即可。
注销设计
添加一个表单,添加一个input按钮,当用户点击此按钮,跳转到一个新的Servlet——LogoutServlet,它要做的事情很简单:
- 获取session对象
- 强制失效session对象
- 重定向主页
效果先看
演示说明:访问主页MainServlet,显示null->访问index.jsp跳转登陆->登陆后跳转主页MainServlet看到用户名->注销,看到null->再次访问index.jsp点击登录直接跳转主页(3天免登陆)
代码
LoginServlet
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static String username; private static String password; private static boolean flag =false;//账号密码是否正确 @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码 req.setCharacterEncoding("utf-8"); //设置响应编码 resp.setContentType("text/html;charset=utf-8"); //获取请求数据 username = req.getParameter("uname"); password = req.getParameter("pwd"); //处理请求 LoginService ls = new LoginServiceImpl(); User user = ls.checkLoginService(username, password); //响应 if(null != user) { //创建cookie,实现3天免登陆 //我们不直接存账号密码,而是存用户的uid Cookie c = new Cookie("uid",user.getUid()+""); //设置有效期为3天 c.setMaxAge(3*24*3600); //设置指定url c.setPath("/200222-CookieLogin/ck"); //添加 resp.addCookie(c); //创建session HttpSession hs = req.getSession(); //设置时效 //将用户对象保存到session中 hs.setAttribute("user", user); resp.sendRedirect("main"); return; }else { //请求转发 req.setAttribute("msg", "账号或密码错误"); req.getRequestDispatcher("page").forward(req, resp); return; } } }
cookieServlet
public class CookieServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码 req.setCharacterEncoding("utf-8"); //设置响应类型及编码 resp.setContentType("text/html;charset=utf-8"); //获取请求 //判断是否有cookie Cookie[] cks = req.getCookies(); //处理请求 if(null!=cks) { //从cookie中获取uid String uid = ""; for(Cookie c : cks) { if("uid".equals(c.getName())) { uid = c.getValue(); } } //校验用户是否存在(使用uid) CookieService ls = new CookieServiceImpl(); User u = ls.checkUidService(uid); if(null!=u) { //设置session HttpSession hs = req.getSession(); //将用户数据保存到session hs.setAttribute("user", u); //跳转主页 resp.sendRedirect("main"); return; }else { req.getRequestDispatcher("/page").forward(req, resp); return; } }else { req.getRequestDispatcher("/page").forward(req, resp); return; } } }
主页MainServlet
public class MainServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码格式 req.setCharacterEncoding("utf-8"); //设置响应类型及编码 resp.setContentType("text/html;charset=utf-8"); //获取session对象 HttpSession hs = req.getSession(); //获取用户对象 User user = (User)hs.getAttribute("user"); //获取用户名 String username = null; if(user!=null) { username = user.getUsername(); } resp.getWriter().write("<html>"); resp.getWriter().write("<head>"); resp.getWriter().write("</head>"); resp.getWriter().write("<body>"); resp.getWriter().write("欢迎登录"+username+"管理系统<hr>"); resp.getWriter().write("<form action='logout' method='get'>"); resp.getWriter().write("<input type='submit' value='退出登录'><br>"); resp.getWriter().write("</form>"); resp.getWriter().write("</body>"); resp.getWriter().write("</html>"); } }
LogoutServlet
public class LogoutServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //强制时效session HttpSession hs = req.getSession(); hs.invalidate(); //重定向到主页 resp.sendRedirect("main"); } }