要实现这个功能只需满足两点:
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;
}
}