1、在登录界面的form表单中加上“记住我”的checkbox;
<input id="remChk" name="remChk" title="记住帐号和密码" type="checkbox" />记住我
2、在登录界面添加获取cookie记录的js;
window.onload = function(){
document.getElementById('userName').value = getCookie('loginUserName');
document.getElementById('password').value = getCookie('loginPassword');
}
var acookie = document.cookie.split("; ");
function getCookie(sname) {
for ( var i = 0; i < acookie.length; i++) {
var arr = acookie[i].split("=");
if (sname == arr[0]) {
if (arr.length > 1 && arr[1] != "\"\""){
return unescape(arr[1]);
}
else{
return null;
}
}
}
}
3、在服务器代码中加入存cookie记录的代码;
// 获取前台数据:记住我checkBox
String remChk = this.getRequest().getParameter("remChk");
// 判断页面是否选中了"记住我"checkBox,并对登录用户信息进行缓存
if (remChk != null && remChk.equals("on"))
{
super.getResponse().addCookie(
new Cookie("loginUserName", loginUserName));
super.getResponse().addCookie(
new Cookie("loginPassword", loginPassword));
}
else
{
super.getResponse().addCookie(new Cookie("loginUserName", ""));
super.getResponse().addCookie(new Cookie("loginPassword", ""));
}