Setting And Reading Cookies

You can easily read and write cookies from both the server and the client side in Vaadin, with one caveat: Cookies are not possible if you enable Push using WebSocket (see tickets 11808 and 12518). Beginning in Vaadin 7.6, cookies can be used with the WEBSOCKET_XHR transport type.

The VaadinRequest class gives easy access to the collection of cookies bundled with each HTTP request. Each cookie is represented as instances of the javax.servlet.http.Cookie class defined by the Java Servlethttp://www.jcp.org/en/jsr/detail?id=315\[spec\].

To read a cookie on the server side you can request the cookies from the current request like so:

Java

  1. // Fetch all cookies
  2. Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();

That will fetch all currently defined cookies. You can then iterate over them to find the cookie you are looking for.

To add a new cookie or update an already defined cookie you can do the following:

Java

  1. // Create a new cookie
  2. Cookie myCookie = new Cookie("cookie-name", "cookie-value");
  3. // Make cookie expire in 2 minutes
  4. myCookie.setMaxAge(120);
  5. // Set the cookie path.
  6. myCookie.setPath(VaadinService.getCurrentRequest().getContextPath());
  7. // Save cookie
  8. VaadinService.getCurrentResponse().addCookie(myCookie);

Here is a full example of utilizing cookies on the server side by storing a a value from a TextField in a cookie for later use.

Java

  1. public class CookieMonsterUI extends UI {
  2. private static final String NAME_COOKIE = "name";
  3. @Override
  4. protected void init(VaadinRequest request) {
  5. final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true);
  6. setContent(layout);
  7. final TextField nameField = new TextField(); layout.addComponent(nameField);
  8. // Read previously stored cookie value
  9. Cookie nameCookie = getCookieByName(NAME_COOKIE);
  10. if (getCookieByName(NAME_COOKIE) != null) {
  11. nameField.setValue(nameCookie.getValue());
  12. }
  13. Button button = new Button("Store name in cookie"); button.addClickListener(new Button.ClickListener() {
  14. @Override
  15. public void buttonClick(ClickEvent event) {
  16. String name = nameField.getValue();
  17. // See if name cookie is already set
  18. Cookie nameCookie = getCookieByName(NAME_COOKIE);
  19. if (nameCookie != null) {
  20. String oldName = nameCookie.getValue();
  21. nameCookie.setValue(name);
  22. Notification.show("Updated name in cookie from " + oldName + " to " + name);
  23. } else {
  24. // Create a new cookie
  25. nameCookie = new Cookie(NAME_COOKIE, name);
  26. nameCookie .setComment("Cookie for storing the name of the user");
  27. Notification.show("Stored name " + name + " in cookie");
  28. }
  29. // Make cookie expire in 2 minutes
  30. nameCookie.setMaxAge(120);
  31. // Set the cookie path.
  32. nameCookie.setPath(VaadinService.getCurrentRequest() .getContextPath());
  33. // Save cookie
  34. VaadinService.getCurrentResponse().addCookie(nameCookie);
  35. }
  36. });
  37. layout.addComponent(button);
  38. }
  39. private Cookie getCookieByName(String name) {
  40. // Fetch all cookies from the request
  41. Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();
  42. // Iterate to find cookie by its name
  43. for (Cookie cookie : cookies) {
  44. if (name.equals(cookie.getName())) {
  45. return cookie;
  46. }
  47. }
  48. return null;
  49. }
  50. }

Finally if you need to read a cookie from client-side code, you can use the Cookies class like so:

Java

  1. // Read name from cookie
  2. String name = Cookies.getCookie("name");
  3. // Write new value to cookie
  4. Cookies.setCookie("name", "Some other value");