Directory

← Back

BrowserCookies

Read and write web browser cookies in a Vaadin application

Author

Rating

Popularity

<100

This add-on implements a server-side component API for reading and writing browser cookies. You can store information in the users browser cookie memory and maintain it across the user sessions. You can use it to implement for example the well-known "Remember me" pattern for login. Now Java 5 Compatible.

Tip: To test with cookie pesistence use the 'restartApplication' URL parameter to start a new session.

Sample code

       // Label for cookie data
        final Label cookieTxt = new Label("[cookies not read]", Label.CONTENT_RAW);
        mainWindow.addComponent(cookieTxt);

        // BrowserCookies widget (by default reads the cookie values)
        final BrowserCookies cookies = new BrowserCookies();
        mainWindow.addComponent(cookies);

        // Listen when cookies are available and read them
        cookies.addListener(new BrowserCookies.UpdateListener() {

            public void cookiesUpdated(BrowserCookies bc) {
                String txt = "";
                for (String name : bc.getCookieNames()) {
                    txt += name + " = '" + bc.getCookie(name) + "'<br />";
                }
                cookieTxt.setValue(txt);
            }
        });
package org.vaadin.browsercookies;

import java.util.Date;

import com.vaadin.Application;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

public class BrowserCookiesApplication extends Application {

    private static final long serialVersionUID = -8873245242163572864L;

    private TextField cookieName;

    private TextField cookieValue;

    private DateField cookieExpire;

    private Button setButton;

    private Label cookieTxt;

    private BrowserCookies cookies;

    @Override
    public void init() {
        Window mainWindow = new Window("BrowserCookies Sample Application");
        Panel cookielistPanel = new Panel("Cookies");
        mainWindow.addComponent(cookielistPanel);
        // Label for cookie data
        cookieTxt = new Label("[cookies not read]", Label.CONTENT_RAW);
        cookielistPanel.addComponent(cookieTxt);

        // BrowserCookies widget (by default reads the cookie values)
        cookies = new BrowserCookies();
        mainWindow.addComponent(cookies);

        // Listen when cookies are available and read them
        cookies.addListener(new BrowserCookies.UpdateListener() {

            public void cookiesUpdated(BrowserCookies bc) {
                String txt = "";
                for (String name : bc.getCookieNames()) {
                    txt += name + " = '" + bc.getCookie(name) + "'<br />";
                }
                cookieTxt.setValue(txt);
            }
        });

        // Panel for setting cookies
        HorizontalLayout hl;
        Panel cookiePanel = new Panel("Set a cookie",
                hl = new HorizontalLayout());
        hl.setMargin(true);
        hl.setSpacing(true);
        mainWindow.addComponent(cookiePanel);

        cookiePanel.addComponent(cookieName = new TextField("Name"));
        cookiePanel.addComponent(cookieValue = new TextField("Value"));
        cookiePanel.addComponent(cookieExpire = new DateField("Expire"));
        cookiePanel.addComponent(setButton = new Button("Set"));
        hl.setComponentAlignment(setButton, Alignment.BOTTOM_LEFT);
        setButton.addListener(new Button.ClickListener() {

            private static final long serialVersionUID = 1L;

            public void buttonClick(ClickEvent event) {
                cookies.setCookie((String) cookieName.getValue(),
                        (String) cookieValue.getValue(), (Date) cookieExpire
                                .getValue());
            }
        });
        setMainWindow(mainWindow);
    }

}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Client-side cookie removal as suggested by 'Vi VK Key' http://vaadin.com/forum/-/message_boards/view_message/18844#_19_message_320293

Released
2011-08-05
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.2+
Browser
Internet Explorer
Internet Explorer
Firefox
Safari
Internet Explorer

BrowserCookies - Vaadin Add-on Directory

Read and write web browser cookies in a Vaadin application BrowserCookies - Vaadin Add-on Directory
This add-on implements a server-side component API for reading and writing browser cookies. You can store information in the users browser cookie memory and maintain it across the user sessions. You can use it to implement for example the well-known "Remember me" pattern for login. Now Java 5 Compatible. Tip: To test with cookie pesistence use the 'restartApplication' URL parameter to start a new session.
Online