一、从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到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. delphi 面向对象实用技能教学二(封装)

    面向对象编程手法,是一项综合技能,单独把谁拿出来说都不合适.本次重写 TSimpleThread ,使其能在 D7 下运行. 基于 TSimpleThread ,重磅推出 TSimpleUI.ExeP ...

  2. node与vue结合的前后端分离跨域问题

    第一点:node作为服务端提供数据接口,vue使用axios访问接口, 安装axios npm install axios --save 安装完成后在main.js中增加一下配置: import ax ...

  3. scrapy-redis源码抛析

    #scrapy-redis--->queue.py-->class FifoQueue 队列 LifoQueue(lastinfirstout栈) #self.server父类Base中链 ...

  4. C#连接MSSQL

    本文将介绍如何用C#连接MSSQL,C#连接SQL十分简单.我们一步一步来操作. 1.打开Microsoft SQL Server Management Studio创建一个数据库,这里我创建一个数据 ...

  5. springmvc 处理器方法返回的是modelandview 重定向到页面

  6. How to watch property in attrs of directive

    Q: I have a controller that has a counter that changes from time to time. That counter is tied to an ...

  7. 关于Bundle对象的思考

    在开发过程中,我们经常使用bundle对象来携带二进制数据,通过INTENT传递出去,那么BUNDLE对象到底是什么?其结构如何? 简要来说,bundle对象类似于一个map,内部是通过<key ...

  8. Codeforces 914C Travelling Salesman and Special Numbers (数位DP)

    题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...

  9. nodejs安装配置

    1.安装node.js(6.3.0)2.检测PATH环境变量是否配置了Node.jscmd下node --version3.D:/www/nodejs文件夹下创建hello.jsvar http = ...

  10. cocos2d-js 序列帧动画

    var spriteCache = cc.spriteFrameCache;spriteCache.addSpriteFrames(res.fireworks_plist,res.fireworks_ ...