coolguy will pay $50.00 to the first person to successfully answer the question:

can you please tell me the code to delete a cookie using java?

unfunded

Answer coolguy's question

People succeed in answering coolguy's questions 0% of the time (0 success in 7 attempts).

Counter Offer:

$60 | $70 | $80 | other:

Answers by: jose | coolguy008 | Meeko28 | JackMann413 | Sir Michael | Rich Collins | JJ

jose's Answer:

Reply by jose 210 days ago

/*
JavaScript Application Cookbook
By Jerry Bradenbaugh

Publisher: O'Reilly
Series: Cookbooks
ISBN: 1-56592-577-7
*/




cookie set, delete, get value and create
<script lanuage='JavaScript'>
// cookies.js
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

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>


Reply by jose 210 days ago

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?

Reply by jose 210 days ago

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()