You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Hans Sowa <ha...@gmail.com> on 2006/01/20 14:08:34 UTC

Problem with cookies

Hi

I try to save and load cookies from my Myfaces projekt. I see that I save
the cookies in the response but if I load the cookies again I can just find
the myfaces cookies but not my ones.

Here is the code which I use:
load code:
    public void ladeCookie() {
        Cookie[] cookies;

        FacesContext faces = FacesContext.getCurrentInstance();

        HttpServletRequest request = (HttpServletRequest) faces
                .getExternalContext().getRequest();

        cookies = request.getCookies();

        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(SessionUser.DATENBANK)) {
                this.setDatenBank(cookies[i].getValue());
            }
        }
    }

save code:
    public void speichereCookies() {
        Cookie c = new Cookie(SessionUser.DATENBANK, this.getDatenBank());
        c.setMaxAge(-1);
        FacesContext faces = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) faces
                .getExternalContext().getResponse();
        response.addCookie(c);

        this.ladeCookie();

    }


Hope this is enough to find the problem.

Thanks in advance.


--
mfg Hans Sowa
mailto:hanssowa@gmail.com

Re: Problem with cookies

Posted by Hans Sowa <ha...@gmail.com>.
Hi David

Thanks for your help.

First the ladeCookie() was just for testing and not part of my Solution.
After your mail I checked my  code again and checked if the cookie is really
saved. In Firefox I could see my cookie but my ladeCookie() function could
not read it. So I checked all attributes of the cookie and found the path
attribute. Here I could see that through a wrong path my code was not able
to read it. I changed the path and now it works.

thanks
Hans

2006/1/23, David G. Friedman <hu...@ix.netcom.com>:
>
> Hans,
>
> Adding a cookie to the Response object does not automatically add it to
> the Request object.  You have to wait until your page finishes so the client
> can receive the cookie. Requests AFTER that will have the cookie.  In your
> code, I see you setting a cookie in speichereCookies()  but immediately
> checking for it in the Request object by invoking ladeCookie() at the end
> of that method.  The problem is you just created the cookie in the
> HttpServletResponse and the client didn't get it yet.  Remember the Request
> and Response objects are not tied together.  Again, the NEXT client submit
> should have your cookie set and readable in the Request object.
>
> Regards,
> David Friedman  / humble@ix.netcom.com
>
>  -----Original Message-----
> *From:* Hans Sowa [mailto:hanssowa@gmail.com]
> *Sent:* Monday, January 23, 2006 5:49 AM
> *To:* MyFaces Discussion
> *Subject:* Re: Problem with cookies
>
> Hi all
>
> Can somebody help me?
>
>
> Thanks in advance.
>
>
> 2006/1/20, Hans Sowa <ha...@gmail.com>:
> >
> > Hi
> >
> > I try to save and load cookies from my Myfaces projekt. I see that I
> > save the cookies in the response but if I load the cookies again I can just
> > find the myfaces cookies but not my ones.
> >
> > Here is the code which I use:
> > load code:
> >     public void ladeCookie() {
> >         Cookie[] cookies;
> >
> >         FacesContext faces = FacesContext.getCurrentInstance();
> >
> >         HttpServletRequest request = (HttpServletRequest) faces
> >                 .getExternalContext().getRequest();
> >
> >         cookies = request.getCookies();
> >
> >         for (int i = 0; i < cookies.length; i++) {
> >             if (cookies[i].getName().equals(SessionUser.DATENBANK)) {
> >                 this.setDatenBank(cookies[i].getValue());
> >             }
> >         }
> >     }
> >
> > save code:
> >     public void speichereCookies() {
> >         Cookie c = new Cookie(SessionUser.DATENBANK, this.getDatenBank
> > ());
> >         c.setMaxAge(-1);
> >         FacesContext faces = FacesContext.getCurrentInstance ();
> >         HttpServletResponse response = (HttpServletResponse) faces
> >                 .getExternalContext().getResponse();
> >         response.addCookie(c);
> >
> >         this.ladeCookie();
> >
> >     }
> >
> > Hope this is enough to find the problem.
> >
> > Thanks in advance.
> >
>


--
mfg Hans Sowa
mailto:hanssowa@gmail.com

RE: Problem with cookies

Posted by "David G. Friedman" <hu...@ix.netcom.com>.
Hans,

Adding a cookie to the Response object does not automatically add it to the
Request object.  You have to wait until your page finishes so the client can
receive the cookie. Requests AFTER that will have the cookie.  In your code,
I see you setting a cookie in speichereCookies()  but immediately checking
for it in the Request object by invoking ladeCookie() at the end of that
method.  The problem is you just created the cookie in the
HttpServletResponse and the client didn't get it yet.  Remember the Request
and Response objects are not tied together.  Again, the NEXT client submit
should have your cookie set and readable in the Request object.

Regards,
David Friedman  / humble@ix.netcom.com

 -----Original Message-----
From: Hans Sowa [mailto:hanssowa@gmail.com]
Sent: Monday, January 23, 2006 5:49 AM
To: MyFaces Discussion
Subject: Re: Problem with cookies


  Hi all

  Can somebody help me?


  Thanks in advance.



  2006/1/20, Hans Sowa <ha...@gmail.com>:
    Hi

    I try to save and load cookies from my Myfaces projekt. I see that I
save the cookies in the response but if I load the cookies again I can just
find the myfaces cookies but not my ones.

    Here is the code which I use:
    load code:
        public void ladeCookie() {
            Cookie[] cookies;

            FacesContext faces = FacesContext.getCurrentInstance();

            HttpServletRequest request = (HttpServletRequest) faces
                    .getExternalContext().getRequest();

            cookies = request.getCookies();

            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(SessionUser.DATENBANK)) {
                    this.setDatenBank(cookies[i].getValue());
                }
            }
        }

    save code:
        public void speichereCookies() {
            Cookie c = new Cookie(SessionUser.DATENBANK,
this.getDatenBank());
            c.setMaxAge(-1);
            FacesContext faces = FacesContext.getCurrentInstance ();
            HttpServletResponse response = (HttpServletResponse) faces
                    .getExternalContext().getResponse();
            response.addCookie(c);

            this.ladeCookie();

        }

    Hope this is enough to find the problem.

    Thanks in advance.

Re: Problem with cookies

Posted by Hans Sowa <ha...@gmail.com>.
Hi all

Can somebody help me?


Thanks in advance.


2006/1/20, Hans Sowa <ha...@gmail.com>:
>
> Hi
>
> I try to save and load cookies from my Myfaces projekt. I see that I save
> the cookies in the response but if I load the cookies again I can just find
> the myfaces cookies but not my ones.
>
> Here is the code which I use:
> load code:
>     public void ladeCookie() {
>         Cookie[] cookies;
>
>         FacesContext faces = FacesContext.getCurrentInstance();
>
>         HttpServletRequest request = (HttpServletRequest) faces
>                 .getExternalContext().getRequest();
>
>         cookies = request.getCookies();
>
>         for (int i = 0; i < cookies.length; i++) {
>             if (cookies[i].getName().equals(SessionUser.DATENBANK)) {
>                 this.setDatenBank(cookies[i].getValue());
>             }
>         }
>     }
>
> save code:
>     public void speichereCookies() {
>         Cookie c = new Cookie(SessionUser.DATENBANK, this.getDatenBank());
>         c.setMaxAge(-1);
>         FacesContext faces = FacesContext.getCurrentInstance ();
>         HttpServletResponse response = (HttpServletResponse) faces
>                 .getExternalContext().getResponse();
>         response.addCookie(c);
>
>         this.ladeCookie();
>
>     }
>
>
> Hope this is enough to find the problem.
>
> Thanks in advance.
>
>
> --
> mfg Hans Sowa
> mailto:hanssowa@gmail.com




--
mfg Hans Sowa
mailto:hanssowa@gmail.com