java 如何删除cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15968276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:28:59  来源:igfitidea点击:

How to remove a cookie

javajqueryservletscookieslogout

提问by Mitaksh Gupta

I am trying to implement the logout functionality for my iPhone app, which uses jQuery mobile, JS at the client side and java at the server side. Currently what I have to do is to clear cookie and redirect to #loginpage tag in my index.html (I have only 1 HTML file in which there are multiple tags for the different pages). What I am doing as of now for the clearCookie is:

我正在尝试为我的 iPhone 应用程序实现注销功能,该应用程序在客户端使用 jQuery mobile、JS,在服务器端使用 java。目前我需要做的是清除 cookie 并重定向到 index.html 中的 #loginpage 标签(我只有 1 个 HTML 文件,其中有不同页面的多个标签)。我现在为 clearCookie 所做的是:

Cookie readCookie = null;
for (Cookie cookie : httpRequest.getCookies()) {
    if (cookie.getName().equals("CookieForLogin")) {
        readCookie = cookie;
        break;
    }
}

readCookie.setMaxAge(0);
httpResponse.addCookie(readCookie);

But this code is not clearing the cookie. I have tried the JS ways, i.e. to set the expiration date to some previous date, given on net but they didn't work too. Also I don't have a response method for HttpServletResponse. How do I clear the cookie that is set at the client side & also how to redirect to a particular tag?

但是这段代码没有清除cookie。我已经尝试过 JS 方法,即将到期日期设置为某个以前的日期,在网上给出,但它们也不起作用。我也没有响应方法HttpServletResponse。如何清除在客户端设置的 cookie 以及如何重定向到特定标签?

回答by BalusC

Cookies are tied to a specific path. You need to make sure that you set the same path during cookie's removal as it was as during cookie's creation. It defaults to the currently requested folder in the URL (and would thus only be available in the same folder or all its subfolders). You'd better explicitly specify the path, otherwise it would be dependent on the currently requested folder in the URL. The cookie path information is like the maxage namely not available in the request cookie header.

Cookie 与特定路径相关联。您需要确保在删除 cookie 期间设置与创建 cookie 期间相同的路径。它默认为 URL 中当前请求的文件夹(因此只能在同一文件夹或其所有子文件夹中使用)。您最好明确指定路径,否则它将依赖于 URL 中当前请求的文件夹。cookie 路径信息就像 maxage 一样,即在请求 cookie 标头中不可用。

Assuming that you created the cookie as follows,

假设您按如下方式创建了 cookie,

Cookie cookie = new Cookie("CookieForLogin", cookieForLogin);
cookie.setPath("/somePath");
cookie.setMaxAge(maxAgeInSeconds);
// ...
response.addCookie(cookie);

it needs to be removed as follows:

它需要被删除如下:

Cookie cookie = new Cookie("CookieForLogin", null);
cookie.setPath("/somePath");
cookie.setMaxAge(0);
// ...
response.addCookie(cookie);

The /somePathis just exemplary. You can also just use /, as long as it's the same in both cases.

/somePath只是示例性的。您也可以只使用/,只要在两种情况下都相同。

Note, the same applies to the Secureand HTTP-onlyflags of the cookie. If you have initially set it to trueduring cookie's creation, then you should also set it to trueduring cookie's removal, they namely defaults to false.

请注意,这同样适用于cookie的SecureHTTP-only标志。如果您最初true在 cookie 创建期间将其设置为,那么您也应该true在 cookie 删除期间将其设置为,即默认为false

That said, I'm not sure how it's useful to store the logged-in user as a cookie. You're basically also allowing the enduser to manipulate its value. Rather just store the logged-in user as a session attribute instead and call session.invalidate()on logout.

也就是说,我不确定将登录用户存储为 cookie 有什么用处。您基本上还允许最终用户操纵其价值。而是将登录用户存储为会话属性,然后调用session.invalidate()注销。

回答by Poorna Senani Gamage

void setMaxAge(int expiry)

void setMaxAge(int expiry)

we can remove cookie using method, known as setMaxAge()

我们可以使用方法删除cookie,称为 setMaxAge()

Cookie c = new Coookie("x", "10");

if you set maxage as 0

如果您将 maxage 设置为 0

c.setMaxAge(0); //it causes the cookie to be deleted.

if you set maxage as negative value

如果您将 maxage 设置为负值

c.setMaxAge(-1);
// cookie is not stored persistently and will be deleted end of the browsing session.

回答by Bhushankumar Lilapara

getMaxAge

获取最大年龄

public int getMaxAge() Gets the maximum age in seconds of this Cookie. By default, -1 is returned, which indicates that the cookie will persist until browser shutdown.

public int getMaxAge() 获取此 Cookie 的最大年龄(以秒为单位)。默认情况下,返回 -1,这表示 cookie 将持续到浏览器关闭。

Returns: an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie persists until browser shutdown See Also: setMaxAge(int)

返回: 一个整数,以秒为单位指定 cookie 的最大年龄;如果为负,则表示 cookie 会一直存在,直到浏览器关闭 另请参阅:setMaxAge(int)

//Java API Documentation

//Java API 文档

IN short you need to set cookie age :

简而言之,您需要设置 cookie 年龄:

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.

 Cookie killMyCookie = new Cookie("mycookie", null);
 killMyCookie.setMaxAge(0);
 killMyCookie.setPath("/");
 response.addCookie(killMyCookie);