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.

SettingAndReadingSessionAttributes.asciidoc 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ---
  2. title: Setting And Reading Session Attributes
  3. order: 9
  4. layout: page
  5. ---
  6. [[setting-and-reading-session-attributes]]
  7. Setting and reading session attributes
  8. --------------------------------------
  9. Vaadin has a few different ways of storing data that should be
  10. accessible later. Which one you should use depends on the scope of the
  11. data, e.g. how long it should be kept around and what parts of the code
  12. should have access to it.
  13. 1. Store the data as a field in your UI subclass. The data is easily
  14. accessible from components belonging to that UI instance and the data
  15. will be gone when the UI is closed.
  16. 2. Store data in the `VaadinServiceSession`. The data is easily
  17. accessible from any UI belonging to the same VaadinServlet or
  18. VaadinPortlet and it will be gone when the session is closed.
  19. 3. Store data in the `HttpSession` or `PortletSession` (represented in
  20. Vaadin through `WrappedSession`). The data is easily accessible from any
  21. part of your web application (i.e. your .war) and it will be gone when
  22. the session is invalidated.
  23. The following example code demonstrates how the data is stored and
  24. retrieved in different ways.
  25. [source,java]
  26. ....
  27. //Remove comment to preserve UI value when reloading
  28. //@PreserveOnRefresh
  29. public class SettingReadingSessionAttributesUI extends UI {
  30. private String value;
  31. private VerticalLayout statusHolder = new VerticalLayout();
  32. private TextField textField = new TextField();
  33. @Override
  34. protected void init(VaadinRequest request) {
  35. addComponent(statusHolder);
  36. addComponent(textField);
  37. addComponent(new Button("Set new values", new Button.ClickListener() {
  38. @Override
  39. public void buttonClick(ClickEvent event) {
  40. String value = textField.getValue();
  41. saveValue(SettingReadingSessionAttributesUI.this, value);
  42. }
  43. }));
  44. addComponent(new Button("Reload page", new Button.ClickListener() {
  45. @Override
  46. public void buttonClick(ClickEvent event) {
  47. getPage().setLocation(getPage().getLocation());
  48. }
  49. }));
  50. showValue(this);
  51. }
  52. private static void saveValue(SettingReadingSessionAttributesUI ui,
  53. String value) {
  54. // Save to UI instance
  55. ui.value = value;
  56. // Save to VaadinServiceSession
  57. ui.getSession().setAttribute("myValue", value);
  58. // Save to HttpSession
  59. VaadinService.getCurrentRequest().getWrappedSession()
  60. .setAttribute("myValue", value);
  61. // Show new values
  62. showValue(ui);
  63. }
  64. private static void showValue(SettingReadingSessionAttributesUI ui) {
  65. ui.statusHolder.removeAllComponents();
  66. ui.statusHolder.addComponent(new Label("Value in UI: " + ui.value));
  67. ui.statusHolder.addComponent(new Label(
  68. "Value in VaadinServiceSession: "
  69. + ui.getSession().getAttribute("myValue")));
  70. ui.statusHolder.addComponent(new Label("Value in HttpSession: "
  71. + VaadinService.getCurrentRequest().getWrappedSession()
  72. .getAttribute("myValue")));
  73. }
  74. }
  75. ....
  76. The UI stores and reads the value in three different ways. The UI
  77. instance value gets lost just by reloading the page as a new UI instance
  78. is then created unless the UI has been marked with `@PreserveOnRefresh`.
  79. If you deploy two different `VaadinServlet` instances using the same UI
  80. class, they will only share the `HttpSession` value.