action中生成cookie:
if (rememberMe != null) { // 创建Cookie
Cookie cokie_user = new Cookie("username", userName);
Cookie cokie_password = new Cookie("password", passWord);
cokie_user.setMaxAge(60 * 60); // 失效时间为一小时
cokie_password.setMaxAge(60 * 60);
response.addCookie(cokie_user);
response.addCookie(cokie_password);
}
jsp中如此调用:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html locale="true">
<head>
<title><bean:message key="loginform.title.displayname"/></title>
</head>
<body>
<%
Cookie[] cook=request.getCookies();
String userid="";
String password="";
if(cook!=null){
for(int i=0;i<cook.length;i++){
if(cook[i].getName().equals("username")){
userid=cook[i].getValue();
}
if(cook[i].getName().equals("password")){
password = cook[i].getValue();
}
}
}
%>
<html:messages id="errors" message="true">
<bean:write name="errors"/>
</html:messages>
<html:javascript formName="LoginForm" cdata="false" />
<br>
<br>
<bean:message key="loginform.title.edit"/>
<bean:message key="loginform.welcome"/>
<html:form action="/login.do" focus="userName" οnsubmit="return validateLoginForm(this);" >
<table width="100%">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<th><bean:message key="loginform.userName.dispalyname"/></th>
<td>
<html:text property="userName" size="20" maxlength="20" value="<%=userid%>"/>
</td>
</tr>
<tr>
<th><bean:message key="loginform.Password.dispalyname"/> </th>
<td>
<html:password property="password" size="20" maxlength="20" value="<%=password%>"/>
</td>
</tr>
<tr>
<td></td>
<td>
<html:checkbox property="rememberMe" value="true">
<bean:message key="loginform.rememberMe.dispalyname"/>
</html:checkbox>
</td>
</tr>
<tr>
<td></td>
<td>
<html:submit styleClass="st" property="submit" >
<bean:message key="loginform.button.login"/>
</html:submit>
</td>
</tr>
</table>
</html:form>
<br>
<br>
<p>
<bean:message key="loginform.message.notmember"/> <a href="profiletil.do" ><bean:message key="loginform.signup.displayname"/> </a>
<bean:message key="loginform.message.account"/>
</p>
<br>
<bean:message key="loginform.message.version"/> · <bean:message key="loginform.message.copyright"/> ©
<bean:message key="loginform.message.produce.date"/>
<a href="http://www.baidu.com"><bean:message key="loginform.message.relation"/></a>
</body>
</html:html>