Setting and reading session attributes

Vaadin has a few different ways of storing data that should be accessible later. Which one you should use depends on the scope of the data, e.g. how long it should be kept around and what parts of the code should have access to it.

  1. Store the data as a field in your UI subclass. The data is easily accessible from components belonging to that UI instance and the data will be gone when the UI is closed.

  2. Store data in the VaadinServiceSession. The data is easily accessible from any UI belonging to the same VaadinServlet or VaadinPortlet and it will be gone when the session is closed.

  3. Store data in the HttpSession or PortletSession (represented in Vaadin through WrappedSession). The data is easily accessible from any part of your web application (i.e. your .war) and it will be gone when the session is invalidated.

The following example code demonstrates how the data is stored and retrieved in different ways.

Java

  1. //Remove comment to preserve UI value when reloading
  2. //@PreserveOnRefresh
  3. public class SettingReadingSessionAttributesUI extends UI {
  4. private String value;
  5. private VerticalLayout statusHolder = new VerticalLayout();
  6. private TextField textField = new TextField();
  7. @Override
  8. protected void init(VaadinRequest request) {
  9. addComponent(statusHolder);
  10. addComponent(textField);
  11. addComponent(new Button("Set new values", new Button.ClickListener() {
  12. @Override
  13. public void buttonClick(ClickEvent event) {
  14. String value = textField.getValue();
  15. saveValue(SettingReadingSessionAttributesUI.this, value);
  16. }
  17. }));
  18. addComponent(new Button("Reload page", new Button.ClickListener() {
  19. @Override
  20. public void buttonClick(ClickEvent event) {
  21. getPage().setLocation(getPage().getLocation());
  22. }
  23. }));
  24. showValue(this);
  25. }
  26. private static void saveValue(SettingReadingSessionAttributesUI ui,
  27. String value) {
  28. // Save to UI instance
  29. ui.value = value;
  30. // Save to VaadinServiceSession
  31. ui.getSession().setAttribute("myValue", value);
  32. // Save to HttpSession
  33. VaadinService.getCurrentRequest().getWrappedSession()
  34. .setAttribute("myValue", value);
  35. // Show new values
  36. showValue(ui);
  37. }
  38. private static void showValue(SettingReadingSessionAttributesUI ui) {
  39. ui.statusHolder.removeAllComponents();
  40. ui.statusHolder.addComponent(new Label("Value in UI: " + ui.value));
  41. ui.statusHolder.addComponent(new Label(
  42. "Value in VaadinServiceSession: "
  43. + ui.getSession().getAttribute("myValue")));
  44. ui.statusHolder.addComponent(new Label("Value in HttpSession: "
  45. + VaadinService.getCurrentRequest().getWrappedSession()
  46. .getAttribute("myValue")));
  47. }
  48. }

The UI stores and reads the value in three different ways. The UI instance value gets lost just by reloading the page as a new UI instance is then created unless the UI has been marked with @PreserveOnRefresh. If you deploy two different VaadinServlet instances using the same UI class, they will only share the HttpSession value.