Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Desperate for Cookie Help

Status
Not open for further replies.

bunnyweb

MIS
Jan 13, 2003
42
IE
I want to update the value of an existing cookie, and I'm having problems. Here is my code:



// Get all the cookies
Cookie [] cookies = request.getCookies();
boolean foundCookie1 = false;

// If there are cookies (to avoid null pointer exception)
if (cookies != null)
{
// Loop through all the cookies to find the right one
for (int index = 0; index < cookies.length; index ++)
{
Cookie cookie1 = cookies[index];
// If the hostname cookie is there
if (cookie1.getName().equals(&quot;hostname&quot;))
{
foundCookie1 = true;
empname = cookie1.getValue();
out.println(&quot;value in cookie before update: &quot; + empname);
String hostnameForCookie = request.getParameter(&quot;hostnameforCookie&quot;);
cookie1.setValue(hostnameForCookie);
}
}
}



My variable &quot;hostnameForCookie&quot; is accurate, because I can output it to the screen.

I am also able to get the value currently in the cookie (before I attempt to update it) so I know I'm accessing the cookie too.

So I can get the value in the cookie, just not update it.

Can someone please help me out? I have been trying to figure this out for days and it's driving me bonkers!!

Thanks a mill!

Rachel
 
After you set the new Cookie value, I think you have to call response.addCookie(cookie1) to return it to the browser. &quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
I tried adding:

response.addCookie(cookie1);

just under

cookie1.setValue(hostnameForCookie);

but it still didn't work.

Thanks for the advice anyway. I'm still praying that someone out there can help me out ...

Rachel
 
worked for me.
Code:
String scookval = request.getParameter(&quot;cookval&quot;);
javax.servlet.http.Cookie c = new javax.servlet.http.Cookie(&quot;hello&quot;,&quot;NONE&quot;);
if( null == scookval)
	scookval = &quot;NONE&quot;;

javax.servlet.http.Cookie[] cooks = request.getCookies();
boolean bfound = false;
for(int n=0; null != cooks && n<cooks.length && !bfound; n++){
	javax.servlet.http.Cookie ctemp = cooks[n];
	if( ctemp.getName().equals(&quot;hello&quot;)){
		System.out.println(&quot;COOKIE CHANGING: &quot; + scookval);
		bfound = true;
		c = ctemp;
		c.setValue( scookval);
		response.addCookie(c);
	}
}
if( !bfound)
	response.addCookie( c);

-pete
 
Well, there's another error somewhere ... Where is the cookie being created to begin with? Since your block of code assumes the cookie is already there, some other code must be creating it. Is it possible another method is being called before or after this section that's resetting the value?

My suggestion is to create a test case that just performs this function and get it working first. With the addition of the addCookie call your code should work as a standalone.

Keep us posted...

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top