一、从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到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. LNMP 1.6 常见的502问题解决

    在nginx上跑discuz,先修改配置文件 cd /usr/local/nginx/conf/vhosts/ vim test.conf server { listen ; server_name ...

  2. 关于JS正则表达式的一篇文章(转载)

    原文:http://www.cnblogs.com/xujh/archive/2008/08/21/1273525.html <input   onkeypress="return   ...

  3. 【Android 多媒体应用】使用 TTS

    import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speec ...

  4. day17 9.关闭资源与异常处理

    Java程序跟任何外部设备进行连接之后,都要把连接断开,把资源释放掉.Connection是一个重量级资源,Connecton占内存,Connection的获取是比较消耗资源和内存的.finally是 ...

  5. 基于unity3d IFC的虚拟仿真系统

  6. UIScrollView现实自动循环滚动

    #import "RootViewController.h" #define width [UIScreen mainScreen].bounds.size.width #defi ...

  7. 项目一:第八天 1、前台系统导入 实现客户注册 发验证码,邮件 springdata-redis存储数据 3、实现客户登陆

    1 前台系统客户注册功能 页面:signup.html 1.1 验证手机号是否注册(邮箱同样) 1. 使用Jquery-validate插件进行相关校验,使用校验规则 <input type=& ...

  8. 用C++实现void reverse(char* str)函数,即反转一个null结尾的字符串.

    void reverse(char* str) { char *end = str, *begin=str; char temp; while(*end!='\0') { end++; } end-- ...

  9. 64位系统中fatal error: stdio.h: 没有那个文件或目录的错误的解决方法

    Ubuntu系统中可输入如下命令,安装开发环境: sudo apt-get install build-essential https://blog.csdn.net/yygydjkthh/artic ...

  10. 修改tomcat默认的编码方式

    tomcat8以后默认编码格式是utf-8:7之前的都是iso8859-1 如果默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找 ...