How to put data in a session variable and get data on another page in a vaadine?

I think I should use the application area session to handle this. But I have no experience with this. I tried different ways that I got from the Internet, for example:

HttpServletRequest request; HttpSession sess = request.getSession(); sess.setAttribute("name", name); later in other page HttpServletRequest request; String=(String)request.getAttribute(name); //or HttpSession sess = request.getSession(); // sess.getAttribute(name); 

everything does not work. I think there might be something special for vaadin. Please help me.

+6
source share
3 answers

Two levels of coverage

See this Roland Krรผger publication on Vaadin 7 coverage with VaadinSession and UI . It includes a comparison with Vaadin 6. A good article, except that the discussion of manual locking is outdated with Vaadin 7.1 (see my comment on this page and see my answer to a similar question).

Understand that although the bulk of Vaadin 6 and Vaadin 7 are the same or the same, on this subject 6 and 7 are completely different, with different architectures and different classes.

Note that we are discussing VaadinSession , not HTTP or a servlet session . VaadinSession wraps or contains a Servlet session, so we donโ€™t need to deal with the Servlet level.

This VaadinSession represents a single user working session. The user has one or more open windows / tabs ( UI instances).

Diagram showing how your entire Vaadin app is tied up in an instance of VaadinSession, and that VaadinSession owns one or more UI instances. State can be placed on either, for two levels of scope (app-wide or one browser window / tab).

This diagram above is a little simplified. Here is more detailed.

Servlet and Vaadin session class hierarchy detail diagram

Multiple windows

Vaadin 7 supports multiple browser windows / tabs open in a single Vaadin application. This is a major architectural change from Vaadin 6.

The contents of each browser window / tab is an instance of your UI subclass. All of these instances belong to the same VaadinSession . If the user clicks on the reload function in the browser tab / tab, the UI instance is destroyed and a new instance is created. But VaadinSession suffers. Adding @PreserveOnRefresh annotation modifies this behavior to keep the same user interface instance, but this is not relevant.

The fact is that the Vaadin 7 has two levels of visibility:

  • VaadinSession (all your application)
  • UI (each browser window / tab).

You might want to save data at any level. For example, user login / authentication information should go to VaadinSession.

Entering UI Status

To save data in the user interface, add fields or collections to your UI subclass. Simple and straightforward.

Enabling VaadinSession

To save data to VaadinSession, call setAttribute and getAttribute . You will have to give results. Simple except for one catch: thread safety.

streaming security

You can manually lock the lock to protect VaadinSession at the same time. But Vaadin will provide this protection against flows if you follow documented rules.

Main theme

If you change VaadinSession from the normal main UI thread, no problem. Most of what you do in Vaadin from the main thread already affects the VaadinSession object. This is where your application lives. Thus, thread-safe locking is already automatically provided by Vaadin.

Other topics

If you use VaadinSession from another thread, make your call in Runnable , go to the access method on either the UI or VaadinSession. If you are using any interface layout or widgets in addition to the session, call the access method on the user interface. If this affects only the session and not the user interface, pass your Runnable access method to VaadinSession.

Third Coverage: Expanded Application Coverage

FYI, you can use a wider reach. If you have global data or objects for sharing applications in all user sessions, you can access the ServletContext object. โ€œContextโ€ means the world of your web application that your web application can access. Your Vaadin application has a single ServletContext object automatically created by your Servlet container. Call a couple of methods, getAttribute and setAttribute , to save whatever Object you have.

For more information, see this question and answer, How to access ServletContext from a Vaadin 7 application? .

To summarize in the text (and the diagram below): a Servlet container, such as Tomcat or Jetty, can run one or more Vaadin web applications. Each web application has a single ServletContext object, which is automatically controlled by the container, where the get/setAttribute can store any objects of your choice. Each Vaadin web application has one VaadinSession object for each current user session. Each VaadinSession object contains one or more instances of a subclass of UI representing the content viewed in a window / tab of web browsers (or in the portlet viewing area). Each instance of a subclass of UI can have any number of member variables, for example, any POJO .

Diagram of Servlet container with one or more Vaadin apps, each with a single ServletContext instance


Tip. Your Runnable can use the new shorter lambda syntax in Java 8 if you want. NetBeans 8 will even suggest that.

+12
source

Vaadin is designed to solve this problem very easily, and it helps not to think about a normal HTTP request / response.

In a typical Vaadin web application (6 or 7) you have a class that handles the first connection to webapp (in 7 you extend the user interface class). From this first connection, all you have to do is save the information you want to save in a regular application: variable, map, list, plain old Java object (POJO), etc. Let's say you use POJO. This object is similar to any other Java object. Keep it in a variable accessible to any object of interest to it. If you add any interactivity to the webapp, the user will again 'enter' the application through this interactive component (say, Button listener). Using this listener, you can access a POJO created earlier in a user session.

What is it. This is actually surprisingly simple and not related to the fact that you've ever touched http (unless you want to later). And, of course, no threadlocals for this storage tier.

+2
source

You can use the ThreadLocal variable to store your data. Then you get the data from the variable and clear it. Remember to make your variable public and static in order to reach it from another class.

+1
source

All Articles