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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie function getMaxAge() doesn't return always the same value!!

Status
Not open for further replies.

t360

Programmer
May 15, 2003
2
US
This JSP code doesn't return the same getMaxAge(). Any idea why, and how can I fix that?


Cookie cookie = new Cookie("test", "some value");
cookie.setMaxAge(86400);
response.addCookie(cookie);
try
{
Cookie[] cookies = request.getCookies();
for (int i=0; i<cookies.length; i++)
if (cookie.getName().equals(&quot;test&quot;))
out.print(&quot;1st result:&quot;+ cookie.getValue() +&quot;___&quot;+ cookie.getMaxAge() +&quot;<br />&quot;);
out.print(&quot;2nd result:&quot;+ cookie.getValue() +&quot;___&quot;+ cookie.getMaxAge() +&quot;<br />&quot;);
}
catch(Exception e)
{
}
 
Arg... I had some typo mistakes. Here is the correct code:


Cookie cookie = new Cookie(&quot;test&quot;, &quot;some value&quot;);
cookie.setMaxAge(86400);
response.addCookie(cookie);
try
{
Cookie[] cookies = request.getCookies();
for (int i=0; i<cookies.length; i++)
{
if (cookies.getName().equals(&quot;test&quot;))
{
out.print(&quot;1st result:&quot;+ cookies.getValue() +&quot;___&quot;+ cookies.getMaxAge() +&quot;<br />&quot;);
}
}
out.print(&quot;2nd result:&quot;+ cookie.getValue() +&quot;___&quot;+ cookie.getMaxAge() +&quot;<br />&quot;);
}
catch(Exception e)
{
}


Result is:
1st result==>gejhgad___-1
2nd result==>gejhgad___86400
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top