一、从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到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( * * );// 设置一年有效期
passwordCookie.setMaxAge( * * );
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( * * );// 设置一年有效期
passwordCookie.setMaxAge( * * );
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="">
<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,直接跳转到主页面,省略了第二步

使用cookie实现自动登录的更多相关文章

  1. cookie实现自动登录

    有很多Web程序中第一次登录后,在一定时间内(如2个小时)再次访问同一个Web程序时就无需再次登录,而是直接进入程序的主界面(仅限于本机).实现这个功能关键就是服务端要识别客户的身份.而用Cookie ...

  2. C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点

    HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...

  3. struts2与cookie实现自动登录和验证码验证

    主要介绍struts2与cookie结合实现自动登录 struts2与cookie结合时要注意采用.action 动作的方式实现cookie的读取 struts2的jar包 链接数据库文件 db.pr ...

  4. 使用cookie下次自动登录

    登录时勾选了自动登录处理: 1.加密账号和IP,保存在cookie中,cookie('auto', $value, $time) 2.解密cookie,取出账号和上次IP,判断上次IP==当前IP.账 ...

  5. cookie技术自动登录

    user public class User implements Serializable{ private String username; private String nick; privat ...

  6. Spring mvc session cookie实现自动登录

    设计过程 1. user表存储用户名密码等信息,login表存放用户登陆状态的表 user表中存储username,pwd,等信息 login表存username,series(UUID),token ...

  7. 如何设计相对安全的cookie自动登录系统

    很多网站登录的时候,都会有一个"记住我"功能,用户可以在限定时间段内免登录, 比如豆瓣.人人.新浪微博等都有这种设计.这种技术其实就是基于 cookie的自动登录, 用户登录的时候 ...

  8. 自己Cookie写的自动登录功能 包含BASE64 和MD5的使用

    sql表 username  password字段 User类 有 id username password等字段 Service有一函数 @Override public User findUser ...

  9. 爬虫模拟cookie自动登录(人人网自动登录)

    什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...

随机推荐

  1. CentOS 7.2 部署Rsync + Lsyncd服务实现文件实时同步/备份 (一)

    接收端配置: 1.安装rsync yum -y install rsync 2.配置同步模块 1. 编辑同步配置文件 vi /etc/rsyncd.conf 2. 同步模块配置参数 # any nam ...

  2. ajax补充FormData

    一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000)  # 3000表示3秒,foo表示一个函数,3秒后 ...

  3. SQL 实现行列互换

    Oracle:不过大多数是采用 oracle 数据库当中的一些便捷函数进行处理,比如 ”pivot”: MySql:目前没有找到更好的方法 题目:数据库中有一张如下所示的表,表名为sales. 年 季 ...

  4. DAY19-Django之form组件补充

    问题1:注册页面输入为空,报错:keyError:找不到password def clean(self): print("---" ,self.cleaned_data) # if ...

  5. dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

    dubbo-2.8.4需用jdk版本为1.8,dubbo-2.5.3可以使用1.7版本的jdk.

  6. css 层叠式样式表(3)

    样式分类 大小 -- 调整div大小,长 width,高 height.长可以直接100%横向沾满屏幕,高不可以. 背景 background-color  背景色 background-image ...

  7. ReentrantLock简单实现2

    ReentrantLock: /** * ReentrantLock测试逻辑类 */ public class MyService { private Lock lock = new Reentran ...

  8. php学习笔记-while循环

    while(condition) { func(); //break the loop } while循环的执行顺序是先判断condition是不是true,如果是true,那么就执行while循环体 ...

  9. GTK编程

    一.简介 GTK(GIMP Toolkit)是一套跨多种平台的图形工具包,按LGPL许可协议发布的.虽然最初是为GIMP写的,但早已发展为一个功能强大.设计灵活的通用图形库.特别是被GNOME选中使得 ...

  10. SDUT 3400 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...