File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Cookies not persistent ... how can it be? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cookies not persistent ... how can it be?" Watch "Cookies not persistent ... how can it be?" New topic
Author

Cookies not persistent ... how can it be?

vik nuckchady
Greenhorn

Joined: Apr 25, 2001
Posts: 25
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Listening
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Vik,
Here is an example code for persistant cookie. PLease try it!
http://www.javaranch.com/ubb/Forum7/HTML/003159.html
regds
maha anna
vik nuckchady
Greenhorn

Joined: Apr 25, 2001
Posts: 25
Hello Anna
I've tried that code you proposed at that link. I still have problems. I tested that code on the following application web servers:
- JRun (Allaire)
- ATG Dynamo (ATG)
- Managed to get it working on (Tomcat)
Believe that the servlet.jar files that come along jRun or ATG were faulty, so i used the one that come with Tomcat but to no avail.

I am not saying that the code is wrong but could you at least test mine on your server or tell me if you know of some configuration that i must set somewhere.
Thanks again for the hand.

Cheers Vik

[This message has been edited by vik nuckchady (edited May 16, 2001).]
[This message has been edited by vik nuckchady (edited May 16, 2001).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Vik,
Verified with JRun3.0 free Developer Edition and able to see all 6 cookies in showCookies.jsp. If I quit browser and restart a new IE browser, able to see only those 3 persistant cookies, which is expected behaviour. Here is the procedure. Please note C:\Program Files\Allaire\JRun is my JRun installation dir.

Please follow these steps and let us know what you get.
regds
maha anna

[This message has been edited by maha anna (edited May 16, 2001).]
vik nuckchady
Greenhorn

Joined: Apr 25, 2001
Posts: 25
Hello Maha Anna
Thanks a lot for helping out.
I think i've found the cause of these problems. Well i am not sure if they are supposed to be like that. But in any case i get the desired result.
Both ATG Dynamo and jRun3.0 they tend to do session management besides being just plain simple dumb application servers . So they have these settings that are defaulted such that you have situations where cookies die after a session.
For example in ATG Dynamo5.1 they have a component called sessionManager where there is a boolean field called usingCookies set to true. In JRun, a simillar field is also set to true etc.
So what i have done is make the required changes and my app seems to operate more or less ok. That is the cookies persist.
I might be wrong in my understanding of that problem. If it is so can you please comment. Otherwise i think that this thread can be closed
Thanks a zillion for your help and concern
Cheers
Vik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cookies not persistent ... how can it be?