如何在用户登录时添加cookie以及如何在用户注销时删除cookie

时间:2013-12-06 12:00:59

标签: cookies

我已在Cookie中使用此代码添加和删除

deleted code:

                    String profileScore=null;
        Cookie cookiesScore =new Cookie("profileScore","");
        cookiesScore.setValue("");
        cookiesScore.setMaxAge(0);
        response.addCookie(cookiesScore);

但未正确删除请帮我举一个例子

1 个答案:

答案 0 :(得分:0)

如果使用javascript

,请尝试此操作

创建一个Cookie:

call a this method at login:

     function setCookie(c_name,value,exdays)
    {
         var exdate=new Date();
         exdate.setDate(exdate.getDate() + exdays);
         var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
         document.cookie=c_name + "=" + c_value;
    }

删除Cookie:

call a this method at logout:

    function deleteCookies() {
        var cookies = document.cookie.split(";");

        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
        }
    }