使用JSP清除所有设置的Cookie

omjgkv6w  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(177)

我想创建一个JSP来删除应用于当前页面的所有cookie(因此,如果重置页面驻留在www.example.com站点上www.dev.example.com,它将获得所有cookie:www.dev.example.com.dev.example.com、. example.com),并在服务器端将它们全部删除。
这个密码是什么?

j8ag8udp

j8ag8udp1#

如果您不想删除,另一种选择是将它们存储为持久cookie

set MaxAge = -1

MaxAge为-1表示您希望Cookie在会话期间保持不变。您希望将MaxAge设置为0。

A negative value means that the cookie is not stored persistently and 
will be deleted when the Web browser exits. A zero value causes the cookie
to be deleted.

另一个是

Iterate all cookies in servlet/Scriptlet and set all Cookies Value NULL .

还有一个

use javascript code to remove all cookies.
sf6xfgos

sf6xfgos2#

引用RFC 2109,只需在HTTP响应中使用Set-Cookie头,并将希望清除的cookie设置为空值。

Set-Cookie: ssid=

更新:我不熟悉JSP,所以我使用PHP和Javascript为例,假设你没有HttpOnly属性在你的cookie。
I use this PHP code to set cookie, you can upload this PHP script to your web server, www.yourserver.com/setcookie.php

<?php
setcookie('name', 'li');
?>

And I use this HTML file to clear your cookies, once again, upload this HTML file to your web server, www.yourserver.com/clearcookie.html

<script>
function deleteAllCookies() {
    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";
    }
}

deleteAllCookies();
</script>
ioekq8ef

ioekq8ef3#

使用以下代码。

<body>

<%

    if(request.getParameter("removeCookie") != null){           
        for(Cookie cookie : request.getCookies()){

            if(!cookie.getName().equals("JSESSIONID")){
                cookie.setMaxAge(0);
                cookie.setValue("");
                cookie.setPath("/");

                response.addCookie(cookie);
            }
        }
    }

%>
<form action="#" method="get">

<input type="submit" id="removeCookie" name="removeCookie" value="Remove cookie" >
</form>

</body>

相关问题