Hi i have a problem with cookies.
I am trying to implement cookies but i cannot get the cookie to remain persistent till after the session (i.e it dies when i close the browser).
I have written the code in
JSP using the Cookie API that i've found on Sun's Site. My application server (the one that parse the jsp and compiles it to a
servlet) is Allaire's JRun.
I've made two JSPs (setCookie.jsp and getCookie.jsp). Here they are:
//---------- setCookie.jsp ----------
<%
// Verifying if cookie already exist on user machine.
Cookie[] c=request.getCookies();
if(c!=null)
{
for(short i=0;i<c.length;i++)
{
if(c[i].getName().equals("tecinCookie"))
{
// Deleting those old cookies.
c[i].setMaxAge(0);
response.addCookie(c[i]);
}
}
}
//Creating a new cookie.
Cookie cookie=new Cookie("myCookie","bla");
// Lifetime of cookie is 3 mins (180s)
cookie.setMaxAge(180);
response.addCookie(cookie);
%>
//---------- getCookie.jsp ----------
<%
Cookie[] c=request.getCookies();
if(c!=null)
{
for(short i=0;i<c.length;i++)
out.println(c[i].getName()+"  "+c[i].getValue
()+"<br>");
}
else
{
out.println("No cookies received");
}
%>
When i run the second JSP (getCookie.jsp) during the same session i only get a "jsessionid" cookie which seems strange.
When i run the second jsp in another session i get no cookies at all (i understand that the above mentioned jsessionid cookie would die).
My problem is that the cookie "myCookie" does not get written at all. Can you explain why is that so.
Thanks in advance.