登录实现记住密码

1.htm页面


<div class="form-actions">
    <label class="checkbox">
        <input type="checkbox" name="remember"/> 记住密码  </label>
    <button type="submit" class="btn blue pull-right">
        登录 <i class="m-icon-swapright m-icon-white"></i>
    </button>
</div>
2.js

<script>
    function getCookie(name)
    {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

    var cookie_user = getCookie("cookie_user");
    if(cookie_user!=null&&cookie_user!=""){
        var username = cookie_user.split("-")[0];
        var password = cookie_user.split("-")[1];
        $('.login-form input[name="userName"]').val(username);
        $('.login-form input[name="password"]').val(password);
        if(password!=""){
            $('input[name="remember"]').attr("checked","checked");
        }
    }
</script>

3.java代码

String v_username = "admin";
String v_password = "admin";

//记住密码
if (remember != null && remember) {
   Cookie cookie = new Cookie("cookie_user", v_username + "-" + v_password);
   cookie.setMaxAge(60 * 60 * 24 * 30); // cookie 保存30天
   cookie.setPath("/");
   response.addCookie(cookie);
} else {
   Cookie cookie = new Cookie("cookie_user", v_username + "-");
   cookie.setMaxAge(60 * 60 * 24 * 30); // cookie 保存30天
   cookie.setPath("/");
   response.addCookie(cookie);
}
注意一点:cookie.setPath("/"),这个不要漏了,不然被拦截了






猜你喜欢

转载自blog.csdn.net/mxlgslcd/article/details/78139470