File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Cookie question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Cookie question" Watch "Cookie question" New topic

Cookie question

Siyaa Hoffman

Joined: Jan 15, 2004
Posts: 23
I am confused between the cookies being set/retrieved in JavaScript and the same in JSP.
When I set a cookie in my JavaScript and then in the same domain, I go to another JSP page. I try to retrieve the cookie from the JSP, it is not there.
Next I tried to set a cookie in the JSP page and then tried to retrieve it from a different html page using JavaScript and I could get the cookie back.
Can somebody please explain me how is it supposed to behave.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Siyaa,
It should not happen. Can you please tell us how you are setting the cookie via Javascript?
Siyaa Hoffman

Joined: Jan 15, 2004
Posts: 23
Hi Maulin,
Thanks for the reply. Following is the JavaScript code which sets the cookie -
(This is setCookie.html which can be accessed by the URL -
function setKookie()
document.Kookie = "version=45";
<form name="frm" action="http://testDRV:8080/doccat/test.jsp">
<input type="submit" value="Submit"/>
and the jsp file which tries to read the cookie is -
and the code to read the cookie is -
Kookie[] cookieArr = request.getKookies();
for(int i=0;i<cookieArr.length;i++)
out.println(cookieArr[i].getName()+" - "+cookieArr[i].getValue());
It does not print the cookie and the cookie length is printed as zero.
(The spelling has been changed intentionally since JavaRanch was not accepting it)
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Siyaa
Sorry for long delay in response..
Well, the kookie setting code doesn't seem complete to me as a kookie has to have many parameters like DOMAIN, PATH etc in usual circumstances
Go to
where you would be able to find how to work with kookies via Javascript.
Also, this link would work well in IE but not in Netscape/Mozilla for unknown reason to me so far Some function links in the left nevigation at the end after letter 'M' don't appear as Hyperlink in Netscape/Mozilla..
I agree. Here's the link:
subject: Cookie question
It's not a secret anymore!