Pleasant94
Pleasant94

Reputation: 511

Vaadin UI - cannot use static fields for every client

I'm creating a web app.

I have a MyUI that extends UI.

    public class MyUI extends UI {

        public static Authentication AUTH;

        @Override
        protected void init(VaadinRequest vaadinRequest) {

            AUTH = new Authentication();

            updateContent();

        }

//other methods

    }

In Authentication() I have the user logged. When I do the logout, that user is set to null.

I noticed that when I access to the server from two device, so when I should have two connection to the server, when someone do the logout, the other one is logged out too.

In Authentication class there is this method:

public void doLogout() {
    System.out.println("User: " + this.user.getMail() + " has logged out.");
    this.user = null;
}

I get error in user.getMail() because the user is already set to null, when I try to do the logout from the other account.

Is com.vaadin.ui.UI unique? How can I handle different connections in Vaadin?

Upvotes: 1

Views: 540

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339669

Java, in general

Basic Java here, nothing to do with Vaadin…

The keyword static means one value per class, otherwise known as a “class variable”, more generally known as a “global variable”. Not object-oriented. Generally speaking, you should minimize your use of static.

Omitting that keyword means “one value per instance of this class”, an instance variable also known as member variables. This is object-oriented.

I suggest you learn more of the basics of Java before embarking on a Vaadin project. And learn the basics of how Java Servlet technology works, perhaps reading the Head First book on Servlet published by O’Reilly (now outdated, but basics are the same). You also need to learn about advanced topics such as threading and concurrency issues as a Servlet environment such as Vaadin is inherently multi-threaded. Eventually, read the book Java Concurrency In Practice by Goetz.

Vaadin specifically

To store values per user of a Vaadin app, use session attributes. This is a key-value store where you insert and retrieve a value by specifying a key, the name of the "attribute". The session is automatically instantiated for you when the user first connects, as part of the Java Servlet technology.

Alternatively, you can store values per user on that UI class as instance variables. Each Vaadin user starts with a UI instance when first connecting. But beware: you can open multiple web browser tabs/windows in a Vaadin 8 app, each having their own UI instance. So to share data between those multiple UI objects, use the session attributes.

Note that while every Vaadin app has at least one UI subclass defined, you can define additional UI subclasses as well, for those additional browser tabs/windows mentioned above to show different content.

Upvotes: 5

pirho
pirho

Reputation: 12235

Maybe instead of static variable you should use here session attribute (updated to be more generic):

    UI.getCurrent().getSession().setAttribute("AUTH", AUTH);
    Authentication auth2 = UI.getCurrent().getSession().getAttribute("AUTH");

So use VaadinSession to store session specific data.

Upvotes: 2

Related Questions