我想删除在我的云服务器中运行的域名和上下文路径为"/“的cookie。
我有下面的代码来清除云服务器中的cookie
Cookie cookie = new Cookie(cookieName, null);// cookieName = TEST_COOKIE
String cookiePath = request.getContextPath();
cookie.setPath(cookiePath); // path = "/"
cookie.setDomain("mydomain.com");
cookie.setMaxAge(0);
response.addCookie(cookie);如果我注意到浏览器中的cookie,我有以下详细信息
cookie name = "TEST_COOKIE" value = "MUZJd3NuNDhy" domain = "mydomain.com" path = "/"在我的本地主机上,上面的代码可以正常工作,不需要设置域名。甚至我也尝试过使用空域名,但这是不起作用的。不知道该怎么做,方向很受欢迎。
编辑-在本地主机中没有域的代码很好地使用上下文路径作为/MyApp。
Cookie cookie = new Cookie(cookieName, null);
String cookiePath = request.getContextPath();
cookie.setPath(cookiePath); // path = "/"
cookie.setMaxAge(0);
response.addCookie(cookie);当我删除contextPath "/MyApp“时,它也停止在本地主机中工作,在云服务器中,上下文路径是"/”。
发布于 2014-09-28 18:03:51
经过大量调试,我发现request.getContextPath在远程服务器和jave中返回空字符串而不是"/“
* Returns the portion of the request URI that indicates the context
* of the request. The context path always comes first in a request
* URI. The path starts with a "/" character but does not end with a "/"
* character. For servlets in the default (root) context, this method
* returns "". The container does not decode this string.由于我有根上下文,方法返回空字符串而不是"/",我已经通过下面的代码修正了它,它现在正在工作。
if (cookiePath.isEmpty()) {
cookie.setPath("/");
} else {
cookie.setPath(cookiePath);
}发布于 2014-09-26 09:16:12
您不能将Cookie设置为null,但可以删除它(即:下次尝试获取它时,它将返回null)。
只需将第一行更改为:
Cookie cookie = new Cookie(cookieName, "");
String cookiePath = request.getContextPath();
cookie.setPath(cookiePath); // path = "/"
cookie.setDomain("mydomain.com");
cookie.setMaxAge(0);
response.addCookie(cookie);https://stackoverflow.com/questions/26055834
复制相似问题