> Markdown version of [Setting And Reading Cookies](https://vaadin.com/docs/v8/framework/articles/SettingAndReadingCookies). Section index: [llms.txt](https://vaadin.com/docs/v8/framework/llms.txt)

# <a id="setting-and-reading-cookies"></a>Setting And Reading Cookies

You can easily read and write [cookies](https://en.wikipedia.org/wiki/HTTP_cookie) 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 [3911](https://github.com/vaadin/framework/issues/3911) and [4411](https://github.com/vaadin/framework/issues/4411)). Beginning in Vaadin 7.6, cookies can be used with the WEBSOCKET\_XHR transport type.

The [VaadinRequest](https://vaadin.com/api/framework/current/com/vaadin/server/VaadinServletRequest.html) class gives easy access to the collection of cookies bundled with each [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) request. Each cookie is represented as instances of the [javax.servlet.http.Cookie](https://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html) class defined by the Java [Servlet](https://en.wikipedia.org/wiki/Java_Servlet)https\://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
// Fetch all cookies
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
// Create a new cookie
Cookie myCookie = new Cookie("cookie-name", "cookie-value");

// Make cookie expire in 2 minutes
myCookie.setMaxAge(120);

// Set the cookie path.
myCookie.setPath(VaadinService.getCurrentRequest().getContextPath());

// Save cookie
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
public class CookieMonsterUI extends UI {

private static final String NAME_COOKIE = "name";

@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true);
setContent(layout);

final TextField nameField = new TextField(); layout.addComponent(nameField);

// Read previously stored cookie value
Cookie nameCookie = getCookieByName(NAME_COOKIE);

if (getCookieByName(NAME_COOKIE) != null) {
  nameField.setValue(nameCookie.getValue());
}

Button button = new Button("Store name in cookie"); button.addClickListener(new Button.ClickListener() {
  @Override
  public void buttonClick(ClickEvent event) {
    String name = nameField.getValue();

    // See if name cookie is already set
    Cookie nameCookie = getCookieByName(NAME_COOKIE);

    if (nameCookie != null) {
      String oldName = nameCookie.getValue();
      nameCookie.setValue(name);
      Notification.show("Updated name in cookie from " + oldName + " to " + name);

    } else {
      // Create a new cookie
      nameCookie = new Cookie(NAME_COOKIE, name);
      nameCookie .setComment("Cookie for storing the name of the user");
      Notification.show("Stored name " + name + " in cookie");
    }

    // Make cookie expire in 2 minutes
    nameCookie.setMaxAge(120);

    // Set the cookie path.
    nameCookie.setPath(VaadinService.getCurrentRequest() .getContextPath());

    // Save cookie
    VaadinService.getCurrentResponse().addCookie(nameCookie);
    }
  });

layout.addComponent(button);

}

private Cookie getCookieByName(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;
  }
}
```

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

```java
// Read name from cookie
String name = Cookies.getCookie("name");

// Write new value to cookie
Cookies.setCookie("name", "Some other value");
```
