wood burning stoves 2.0*
The moose likes Servlets and the fly likes Writing Cookies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Writing Cookies" Watch "Writing Cookies" New topic
Author

Writing Cookies

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I'm writing some code to write a cookie.. but its not happening. Can someone give me example of code to write a cookie. I even created the cookie and then added it to the response.. but nothing. Any help is apprecaited.


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
To create a cookie that stores a user name retrieved from a request parameter:To get all the cookie from the client and print them to the screen:
By the way, if you are curious about the getKookies() method in the above code, it is that way because the bulletin board security would not let me type in the correct method name (replace the K with a C).
[ January 03, 2005: Message edited by: Paul Bourdeaux ]

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Do these need to happen in a Servlet or can they happen in a jsp page as well?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can do the same thing from a JSP page.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
You can do it from a jsp with scripting... just include the same code inside the <% and >% tags. JSPs have both a request and response implicit object. You can also get (but not set) cookies VERY easily using EL. EL has an implicit object called cookie that is a map of cookie names/values. ${cookie.username.value} will return the value for the cookie named "username".

By the way, I personally wouldn't set any cookies from a jsp anyway...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing Cookies
 
Similar Threads
How to Submit form on load event of body/window
Not able to post
Persistent authentication
Cookie Code Not Working
why there is no getCookie(cookie name) function?