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

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