struts COOKIE 记住帐号或密码

要实现这个功能只需满足两点:

1、在控制器中将信息加入到Cookie中

2、在前台页面中遍历出cookie即可。

遍历有很多种方法,推荐采用jstl的方式,最好不要加入java脚本。

UserAction.java


 

package com.wzy.action;

import javax.servlet.http.Cookie;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String user_login() {
		Cookie username = new Cookie("username", getUsername());
		Cookie password = new Cookie("password", getPassword());
		username.setMaxAge(60*60*24*60);//周期是60天
		password.setMaxAge(60*60*24*60);//周期是60天
		ServletActionContext.getResponse().addCookie(username);
		ServletActionContext.getResponse().addCookie(password);
		return SUCCESS;
	}
}


 

cookie.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@page isELIgnored="false" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>struts COOKIE 记住帐号或密码</title>
</head>
<body>
<form action="user_login_go.action" name="myform">
  	<c:forEach items="${pageContext.request.cookies}" var="c">
		<c:if test="${c.name=='username'}">
		 用户名:<input type="text" name="username" value="${c.value}">
		</c:if>
		<c:if test="${c.name=='password'}">
		密码: <input type="password" name="password" value="${c.value}">
		</c:if>
    </c:forEach>
    <input type="submit" value="submit"/>
</form>
</body>
</html>

 

以上代码就实现了记住密码的功能。


有网友发布了一个cookie的工具类,可以参考下

CookieUtils.java

package cn.edu.pdsu.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import cn.edu.pdsu.action.LoginAction;
import cn.edu.pdsu.bean.User;
import cn.edu.pdsu.dao.UserDao;

/**
 * cookie的增加、删除、查询
 */
public class CookieUtils {
	public static final String USER_COOKIE = "user.cookie";

	// 添加一个cookie
	public Cookie addCookie(User user) {
		Cookie cookie = new Cookie(USER_COOKIE, user.getUsername() + ","
				+ user.getPassword());
		System.out.println("添加cookie");
		cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周
		return cookie;
	}

	// 得到cookie
	public boolean getCookie(HttpServletRequest request, UserDao userDAO) {
		Cookie[] cookies = request.getCookies();
		System.out.println("cookies: " + cookies);
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				System.out.println("cookie: " + cookie.getName());
				if (CookieUtils.USER_COOKIE.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (StringUtils.isNotBlank(value)) {
						String[] split = value.split(",");
						String username = split[0];
						String password = split[1];
						User user = userDAO.checkUser(username, password);
						if (user != null) {
							HttpSession session = request.getSession();
							session.setAttribute(LoginAction.USER_SESSION, user);// 添加用户到session中
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	// 删除cookie
	public Cookie delCookie(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (USER_COOKIE.equals(cookie.getName())) {
					cookie.setValue("");
					cookie.setMaxAge(0);
					return cookie;
				}
			}
		}
		return null;
	}
}


 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值