can you please tell me the code to delete a cookie using java?
unfundedPeople succeed in answering coolguy's questions 0% of the time (0 success in 7 attempts).
Answers by: jose | coolguy008 | Meeko28 | JackMann413 | Sir Michael | Rich Collins | JJ
/*
JavaScript Application Cookbook
By Jerry Bradenbaugh
Publisher: O'Reilly
Series: Cookbooks
ISBN: 1-56592-577-7
*/
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
</script>
<script language='JavaScript'>
<!--</p>
<p>if (GetCookie('user_id') == null) {
<br /> var getName = prompt('Hi... First time, huh?? We all have to through it. Please enter your name.', '');
<br /> document.writeln('<H2>Welcome, ' + (getName != '' ? getName : 'Anonymous user') + '</H2>' +
<br /> 'This is your first visit.');
<br /> SetCookie('user_id', (getName != '' ? getName : 'Anonymous user'), expiry);
<br /> SetCookie('hit_count', '2', expiry);
<br /> }
<br />else {
<br /> var getName = GetCookie('user_id');
<br /> var getHits = GetCookie('hit_count');
<br /> document.writeln('<H2>Welcome Back, ' + getName + '</H2>' +
<br /> 'You have visited ' + getHits + ' times.');
<br /> getHits = parseInt(getHits) + 1;
<br /> SetCookie('hit_count', '' + getHits + '', expiry);
<br /> }</p>
<p>//-->
</script>
Here's how I delete a cookie:
Cookie cart = new Cookie( "CART", null );
cart.setMaxAge( 0 );
resp.addCookie( cart );
Also, your wording concerns me. You say you still see the cookie
"in the cookie file". Perhaps your browser internally caches the
cookie jar and only writes it out when exiting?
HOW WE'RE DELETING COOKIES:
protected void deleteCookie(String cookieName)
{
String cookieDomain =
CTPropertiesManager.getProperty("site.properties",
"site.cookie.domain");
try
{
//get all cookies
Cookie cookies[] = request.getCookies();
Cookie ctCookie=null;
if (cookies !=null)
{
for(int i=0; i
{
ctCookie=cookies[i];
if (ctCookie.getName().trim().equals(cookieName))
{
if ( cookieDomain != null )
{
ctCookie.setDomain(cookieDomain);
}
ctCookie.setPath("/ct");
ctCookie.setMaxAge(0);
response.addCookie(ctCookie);
}
}//end for
}//end if cookie
}//end try
catch(Exception e){
CTLogManager.log(e);
}
}//end deleteCookie()