jsp代码:
<
br
>
<
input
type
="text"
id
="userName"
name
="userName"
value
="${username }"
placeholder
="用户名">
<
br
>
<
input
type
="password"
id
="pwd"
name
="pwd"
maxLength
="50"
value
="${password }"
placeholder
="密码">
<
br
>
<
label
for
="remember-me">
<
input
name="rember" id="rember" οnclick="remember();" /
>
记住密码
</
label
>
JS代码:
$(document).ready(
function
(){
//记住密码功能
var str = getCookie("loginInfo"
);
str = str.substring(1,str.length-1);
var username = str.split(",")[0
];
var password = str.split(",")[1
];
//自动填充用户名和密码
$("#userName"
).val(username);
$("#pwd").val(password);});
//获取cookie
function
getCookie(cname) {
var name = cname + "="
;
var ca = document.cookie.split(';'
);
for(var i=0; i<ca.length; i++
) {
var c =
ca[i];
while (c.charAt(0)==' ') c = c.substring(1
);
if (c.indexOf(name) != -1) return
c.substring(name.length, c.length);
}
return ""
;}
//记住密码功能
function
remember(){
var remFlag = $("input[type='checkbox']").is(':checked'
);
if(remFlag==true){
//如果选中设置remFlag为1
//cookie存用户名和密码,回显的是真实的用户名和密码,存在安全问题.
var conFlag = confirm("记录密码功能不宜在公共场所(如网吧等)使用,以防密码泄露.您确定要使用此功能吗?"
);
if(conFlag){
//确认标志
$("#rember").val("1"
);
}
else
{
$("input[type='checkbox']").removeAttr('checked');
$("#rember").val("");
}
}
else{
//如果没选中设置remFlag为""
$("#rember").val(""
);
}
}
Java后台代码:
//获取前台输入的用户名
String name = request.getParameter("userName");
//获取前台输入的密码
String passWord = request.getParameter("pwd");
//获取前台 记住用户名、密码功能
String remFlag = request.getParameter("rember");
if
("1".equals(rember)){
//"1"表示用户勾选记住密码
String loginInfo = name+","+passWord;
Cookie userCookie=
new
Cookie("loginInfo",loginInfo);
//存活期为一个月 30*24*60*60
userCookie.setMaxAge(30*24*60*60);
userCookie.setPath("/");
response.addCookie(userCookie);
}