You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SettingAndReadingCookies.asciidoc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ---
  2. title: Setting And Reading Cookies
  3. order: 16
  4. layout: page
  5. ---
  6. [[setting-and-reading-cookies]]
  7. = Setting And Reading Cookies
  8. You can easily read and write
  9. http://en.wikipedia.org/wiki/HTTP_cookie[cookies] from both the server
  10. and the client side in Vaadin, with one caveat: Cookies are not possible
  11. if you enable Push using WebSocket (see tickets
  12. http://dev.vaadin.com/ticket/11808[11808] and
  13. http://dev.vaadin.com/ticket/12518[12518]). Beginning in Vaadin 7.6,
  14. cookies can be used with the WEBSOCKET_XHR transport type.
  15. The
  16. https://vaadin.com/api/7.0.3/com/vaadin/server/VaadinRequest.html[VaadinRequest]
  17. class gives easy access to the collection of cookies bundled with each
  18. http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol[HTTP] request.
  19. Each cookie is represented as instances of the
  20. http://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html[javax.servlet.http.Cookie]
  21. class defined by the Java
  22. http://en.wikipedia.org/wiki/Java_Servlet[Servlet]http://www.jcp.org/en/jsr/detail?id=315[spec].
  23. To read a cookie on the server side you can request the cookies from the
  24. current request like so:
  25. [source,java]
  26. ....
  27. // Fetch all cookies
  28. Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();
  29. ....
  30. _That will fetch all currently defined cookies. You can then iterate
  31. over them to find the cookie you are looking for._
  32. To add a new cookie or update an already defined cookie you can do the
  33. following:
  34. [source,java]
  35. ....
  36. // Create a new cookie
  37. Cookie myCookie = new Cookie("cookie-name", "cookie-value");
  38. // Make cookie expire in 2 minutes
  39. myCookie.setMaxAge(120);
  40. // Set the cookie path.
  41. myCookie.setPath(VaadinService.getCurrentRequest().getContextPath());
  42. // Save cookie
  43. VaadinService.getCurrentResponse().addCookie(myCookie);
  44. ....
  45. Here is a full example of utilizing cookies on the server side by
  46. storing a a value from a `TextField` in a cookie for later use.
  47. [source,java]
  48. ....
  49. public class CookieMonsterUI extends UI {
  50. private static final String NAME_COOKIE = "name";
  51. @Override
  52. protected void init(VaadinRequest request) {
  53. final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true);
  54. setContent(layout);
  55. final TextField nameField = new TextField(); layout.addComponent(nameField);
  56. // Read previously stored cookie value
  57. Cookie nameCookie = getCookieByName(NAME_COOKIE);
  58. if (getCookieByName(NAME_COOKIE) != null) {
  59. nameField.setValue(nameCookie.getValue());
  60. }
  61. Button button = new Button("Store name in cookie"); button.addClickListener(new Button.ClickListener() {
  62. @Override
  63. public void buttonClick(ClickEvent event) {
  64. String name = nameField.getValue();
  65. // See if name cookie is already set
  66. Cookie nameCookie = getCookieByName(NAME_COOKIE);
  67. if (nameCookie != null) {
  68. String oldName = nameCookie.getValue();
  69. nameCookie.setValue(name);
  70. Notification.show("Updated name in cookie from " + oldName + " to " + name);
  71. } else {
  72. // Create a new cookie
  73. nameCookie = new Cookie(NAME_COOKIE, name);
  74. nameCookie .setComment("Cookie for storing the name of the user");
  75. Notification.show("Stored name " + name + " in cookie");
  76. }
  77. // Make cookie expire in 2 minutes
  78. nameCookie.setMaxAge(120);
  79. // Set the cookie path.
  80. nameCookie.setPath(VaadinService.getCurrentRequest() .getContextPath());
  81. // Save cookie
  82. VaadinService.getCurrentResponse().addCookie(nameCookie);
  83. }
  84. });
  85. layout.addComponent(button);
  86. }
  87. private Cookie getCookieByName(String name) {
  88. // Fetch all cookies from the request
  89. Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();
  90. // Iterate to find cookie by its name
  91. for (Cookie cookie : cookies) {
  92. if (name.equals(cookie.getName())) {
  93. return cookie;
  94. }
  95. }
  96. return null;
  97. }
  98. }
  99. ....
  100. Finally if you need to read a cookie from client-side code, you can use
  101. the `Cookies` class like so: 
  102. [source,java]
  103. ....
  104. // Read name from cookie
  105. String name = Cookies.getCookie("name");
  106. // Write new value to cookie
  107. Cookies.setCookie("name", "Some other value");
  108. ....