环境:
需要一个jquery-cookie.js和jquery
jquery-cookie和jquery
密码: sa6v
实现步骤
1.在验证servlet中创建cookie
- 创建cookie
- 设置cookie存活时间
- 加入页面
//代码实现
Cookie c_username = new Cookie("username", username);//创建cookie,username值由前端输入获取
Cookie c_password = new Cookie("password",password);
c_username.setMaxAge(24*60*60);//设置存活时间
c_password.setMaxAge(24*60*60);
resp.addCookie(c_username);//加入到浏览器缓存中
resp.addCookie(c_password);
2.前端jsp页面设置cookie
- 根据checkbox便签状态进行设置
- 如果checked为true,更改输入框内容
- 如果checked为false,清除输入框内容
//cookie实现存储密码
$("#checkboxId").click(function(){
var username = $.cookie("username");//获取input输入框
var password = $.cookie("password");
if(this.checked){//当checked为选中时执行
$("#usernameId").val(username);//进行设置值
$("#passwordId").val(password);
}else{//当cheked未被选中时清空
$("#usernameId").val("");
$("#passwordId").val("");
}
});