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


Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
I'm setting a cookie using:
Cookie cook = new Cookie("Key","This Value");

Right now I have to do the following to retrieve the value of the Cookie:

PrintWriter out = resp.getWriter();
Cookie cookie[] = req.getKookies();
for(int i=0;i<cookie.length;i++)

How do I retrieve the value of the Cookie using the key or name of the cookie?

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Whats wrong with using the Cookie getName() method?
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
Using the Cookie getName() is fine. But do I have to iterate through all the cookies and use an if equals to cookie name logic to get the cookie that I want? Or can I use the getName() method to get the value of the cookie that I want directly?

Could you please write a little piece of code that retrieves a cookie by using the Cookie's name.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I didn't test it but you should get the idea.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Cookies
It's not a secret anymore!