一、從登錄——>主頁面,進行的過程是,輸入 用戶名和密碼,以及驗證碼,點擊“登錄”跳轉到Activity.jsp
login1.action(跳轉到登錄頁面)
/** 跳轉到login(有積分排行榜) */ @RequestMapping("/login1.action") public String login() { return "login"; }
login.action(從登錄頁面跳轉到主頁面)
/** 登錄 */ @RequestMapping("/login.action") public String login(String nickName, String password, String authCode, String autoLogin, HttpSession session, Model model, HttpServletRequest req, HttpServletResponse resp) { System.out.println("autoLogin:" + autoLogin);//自動登錄多選框狀態,未選中時為null,選中時為on // 登錄積分和等級 PointAction loginPoint = null; Graderecord loginLevel = null; if (authCode == null || authCode == "") { model.addAttribute("msg", "請填寫驗證碼!"); return "login"; } if (!authCode.equals(session.getAttribute("authCode"))) { model.addAttribute("msg", "驗證碼錯誤"); return "login"; } try { // 根據頁面用戶名查詢用戶信息 Memberinfo memberinfo = memberservice.loginMemberInfo(nickName); session.setAttribute("nickName", nickName); // 判斷密碼是否正確 if (password.equals(memberinfo.getPassword())) { memberservice.loginAction(memberinfo, loginPoint, session, loginLevel); if (autoLogin != null) { // 保存cookie try { Cookie usernameCookie = new Cookie("nickname", URLEncoder.encode(nickName, "utf-8")); Cookie passwordCookie = new Cookie("password", password); usernameCookie.setMaxAge(360 * 24 * 60);// 設置一年有效期 passwordCookie.setMaxAge(360 * 24 * 60); usernameCookie.setPath("/");// 可在同一應用服務器內共享方法 passwordCookie.setPath("/"); resp.addCookie(usernameCookie); resp.addCookie(passwordCookie); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return "activity"; } else { model.addAttribute("msg", "請輸入正確密碼"); } return "login"; } catch (MemberServiceException e) { e.printStackTrace(); model.addAttribute("msg", e.getMessage()); return "login"; } }
在此時,進行Cookie的保存,即中間的這一段代碼
if (autoLogin != null) {//判斷自動登錄多選框的狀態,若選中則進行Cookie的保存 // 保存cookie try { Cookie usernameCookie = new Cookie("nickname", URLEncoder.encode(nickName, "utf-8")); Cookie passwordCookie = new Cookie("password", password); usernameCookie.setMaxAge(360 * 24 * 60);// 設置一年有效期 passwordCookie.setMaxAge(360 * 24 * 60); usernameCookie.setPath("/");// 可在同一應用服務器內共享方法 passwordCookie.setPath("/"); resp.addCookie(usernameCookie); resp.addCookie(passwordCookie); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return "activity";
在index.jsp頁面中調用checkAutoLoginAction.action
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="refresh" content="0;url='checkAutoLoginAction.action'"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> </body> </html>
checkAutoLoginAction.action(取出Cookie
// ------自動登錄---------------------------------------------------------------------------------------------------------------------------- @RequestMapping("checkAutoLoginAction.action") public String checkAutoLoginAction(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws Exception { Cookie[] cookies = req.getCookies(); System.out.println("cookie是否為空:" + cookies); String nickname = ""; String password = ""; if (cookies != null) {//判斷Cookie是否為空 for (Cookie c : cookies) { if ("nickname".equals(c.getName())) { nickname = URLDecoder.decode(c.getValue(), "utf-8"); } if ("password".equals(c.getName())) { password = URLDecoder.decode(c.getValue(), "utf-8"); } } Memberinfo m = memberservice.login(nickname, password); session.setAttribute("nickName", m.getNickName()); System.out.println("m是否為空:" + m); if (m != null) {//如果根據Cookie中的用戶名和密碼查詢出的用戶信息存在且正確,再進行一系列的更新跳轉工作 Calendar c = Calendar.getInstance(); c.setTime(m.getLatestDate()); String date = new SimpleDateFormat("EEEE").format(c.getTime()); return "activity"; } } req.setAttribute("msg", "賬戶密碼失效,請重新登錄"); return "forward:/login1.action"; }
三、操作
第一步,輸入http://localhost:8888/ssh/login1.action,跳轉到登錄頁面
第二步,輸入nickName和password,勾選“自動登錄”,點擊“登錄”,跳轉到Activity.jsp主頁面
第三步,若成功登錄到主頁面,則注銷
第四步,輸入http://localhost:8888/ssh/index,即可使用checkAutoLoginAction.action,直接跳轉到主頁面,省略了第二步