如何设计登录界面“记住我”的功能?

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", ""));
        }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值