You have to set any cookies before sending any content.  The
reason is because cookies appear in the HTTP header.  If
you've sent content, then implicitly the header must already
have been sent, so it's too late for a cookie.

You may also want to actually look at the HTTP headers the server
is sending back.  The int you put in for the cookie expiration
time is translated into a timestamp inside the Set-Cookie header.
If you look at this timestamp directly, you can tell if it is
a valid date.

> -----Original Message-----
> From: A mailing list about Java Server Pages specification
> and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Ethan Henry
> Sent: Friday, August 13, 1999 6:39 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Can't make cookies persist
>
>
> Nicholas Barrington wrote:
> >
> > You need to set the cookie expiration time.  This is not a
> JSP issue but to
> > do with all cookies.  If you want cookies to last a certain
> time you need to
> > tell the client machine to store them that long.
>
> I have to apologize for not being clear enough in my initial email.
>
> I have set cookie.setMaxAge() to Integer.MAX_INT - no fool am I.
> (Perhaps MAX_INT is too big?)
>
> It still, however, doesn't persist past the lifetime of a
> single browser
> session.
>
> I also realize that it's not a JSP-specific issue, but I'm
> trying to set the
> cookie from within a JSP page, so I asked here first. From a
> rough search, I
> didn't find anything useful in the archives of either the
> servlet-interest or
> jsp-interest mailing list. (at http://archives.java.sun.com)
> Cookies should be
> pretty simple, so I was wondering if there's anything
> undocumented that you
> have to do, like set the path or domain explicitly.
>
> Perhaps someone could post a code fragment they have that
> successfully gets a
> cookie to be added to the cookie file so I can compare with
> my own code, which
> looks like this...
>
> cookie = new Cookie("name",request.getParameter("name"));
> cookie.setMaxAge(Integer.MAX_VALUE);
> cookie.setPath("/");
> response.addCookie(cookie);
>
> If I add the cookie to the response, do I have to do so before writing
> anything to "out"? I may be having a problem with HTML code
> in the generates
> servlet being written out before I create my cookies. Investigation
> continues...
>
> --
> Ethan Henry                                            [EMAIL PROTECTED]
> Java Evangelist, KL Group                       http://www.klg.com
>                "Software Development Productivity"
>
> ==============================================================
> =============
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body
> of the message "signoff JSP-INTEREST".  For general help,
> send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
For JSP FAQ, http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to