Cookie again...

Gentlemen, good morning. I’ll ask a question that I have not seen answer: You can use cookie in Vaadin 7?
How?
I tried using various solutions presented here and none worked. Thanks for the help …
a hug

Hi,

did you check out
https://vaadin.com/wiki/-/wiki/Main/Setting+and+reading+Cookies
?

Note that using websockets for push will most likely prevent setting cookies.

Hello, Teppo. As I said, I’ve searched a lot for this help on the internet. Not found. My big problem is that I need to use cookie and push. My dumb question: is posível use cookie and push in Vaadin 7?
Thank you.
a hug

Hi,

unfortunately that is currently impossible. Due to the way cookies work in browsers, you will need a “real” request to set or read a cookie. See e.g. these tickets:
http://dev.vaadin.com/ticket/12518
and
http://dev.vaadin.com/ticket/13459
.

If you only need to set the cookie at the session start, you could disable push, set the cookie and enable push some time after that.

OK, Teppo, liked the idea. Implemented and can now get the Response object. I do not get the error message. But the cookie is not recorded. Use this code:

Cookie nameCookie = getCookieByName (NAME_COOKIE);
nameCookie.setPath (VaadinService.getCurrentRequest ()
. getContextPath ()); VaadinService.getCurrentResponse () addCookie (nameCookie).;

Thanks for the help.
a hug

Hi

I assume you’re trying to create a new cookie? The first line of your code is trying to fetch an existing cookie. If you’ve not set it previously it will not exist. Can you try something like this:

Cookie nameCookie = new Cookie("name", "value");
nameCookie.setPath(VaadinService.getCurrentRequest().getContextPath());
VaadinService.getCurrentResponse().addCookie(nameCookie);

If you later need to read back the cookie in your vaadin code, then you should use the getCookieByName method.

-tepi

Sure , Temp , sorry if I did not put all the code.
I create the cookie this way :

NameCookie cookie = new Cookie ( "name " , "value " ) ;
nameCookie.setPath ( VaadinService.getCurrentRequest () getContextPath ( .) );
VaadinService.getCurrentResponse () addCookie ( nameCookie ) . ;

Do not get the error message.
But when I access it with code:

Cookie nameCookie = getCookieByName ( NAME_COOKIE );
nameCookie.setPath ( VaadinService.getCurrentRequest ()
. getContextPath ()); VaadinService.getCurrentResponse () addCookie ( nameCookie ) . ;

and

private getCookieByName Cookie ( String name ) {
/ / Fetch all cookies from the request
Cookie cookies = VaadinService.getCurrentRequest ()
. getCookies ();

    / / Iterate to find cookie by its name
    for ( Cookie cookie : cookies ) {
        if ( name.equals ( cookie.getName () )) {
            return cookie;
        }
    }

    return null;
}

I can not find it . I’ve tried using ‘/’ as path and unsuccessful.
Is there any rule for the cookie name ?
Is there any rule for the cookie expires ?

Many thanks for the help .
a hug