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

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