一、从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到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. 2016.2.28 DataTable用法汇总

    将控件的DataSource转换为DataTable,但是,此控件的DataSource绑定时必须是DataTable,不能是List DataTable dt = (bgvRoutePortion. ...

  2. DDD学习笔录——领域驱动设计DDD概念总结

  3. xcode减小静态库的大小(转)

    减小静态库的大小 编译生成的.a文件太大,但又没有冗余的文件可以删除已减少体积,找了很久才找到解决办法,如下: Build Settings-->Generate Debug Symbols 将 ...

  4. CSS框模型:一切皆为框 — 从行框说起

    一 行框 看图说话 css 行框 各部分详解 上图代表了框模型中的行框.line-height 属性设置行间的距离(行高).该属性会影响行框的布局.在应用到一个块级元素时,它定义了该元素中基线之间的最 ...

  5. [poj3159]Candies(差分约束+链式前向星dijkstra模板)

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  6. NLTK词性标注解释

    1.      CC      Coordinating conjunction 连接词2.     CD     Cardinal number  基数词3.     DT     Determin ...

  7. Codeforces 1076E Vasya and a Tree(树状数组)或dfs

    题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...

  8. SimpleDateFormat-多线程问题

    SimpleDateFormat-多线程问题: SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况 import java.text.ParseException; ...

  9. Tensorflow梯度下降应用

    import tensorflow as tfimport numpy as np #使用numpy生成随机点x_data = np.random.rand(100)y_data = x_data*0 ...

  10. session跨域共享

    www.maxomnis.com的index.php文件内容 <?phpsession_start();setcookie("user", "alex proter ...