Multiple UI not sharing same session

Hello All,

I’m a little bit confused about using multiple UI in the same application.
I made a few tests and never got it to work.

According to documentation, in Vaadin7, we can manage multiple UIs in the same application, they sould all share the same session. According to my tests, the session is never the same.

A quick and dirty test was :
One principal UI :

public class Vaadin_testUI extends UI {


    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(
        productionMode = false,
        ui = Vaadin_testUI.class)
    public static class testUIServlet extends VaadinServlet
    {
    }
    
    Table table;
    
    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.addComponent(new Label("MainUI"));
        setContent(layout);
   }
}

Then a secondary UI :

public class ExternalWindowUI extends UI
{

    @WebServlet(value = "/external/*", asyncSupported = true)
    @VaadinServletConfiguration(
        productionMode = false,
        ui = ExternalWindowUI.class)
    public static class MyProjectServlet extends VaadinServlet {
    }
    
    @Override
    protected void init(VaadinRequest request)
    {

        VerticalLayout vLayout = new VerticalLayout();
        vLayout.addComponent(new Label("TEST"));
        
        Button btn = new Button("TEST");
        btn.addClickListener(new Button.ClickListener() {
            
            @Override
            public void buttonClick(ClickEvent event) {
            
                Notification.show("UIs : " + getSession().getUIs().size());
                
            }
        });
        vLayout.addComponent(btn);
        
        setContent(vLayout);
        
        
    }

}

Calling main UI in the browser shows me the “MainUI”
Calling secondary UI in another tab in the browser shows me “TEST” button.
Both UI are working. But if I click the button, i expect to get “UIs : 2” (main and secondary). Instead I’m getting “UIs : 1”
If I debug and check getSession() values on each UIs, they are different.

What am I missing here ?

Regards
Sebastien

Hi Sebastien,

The problem is that you have two servlets. Even though all the servlets in the same “web application” do share the same HTTP session (per user), VaadinSession instances inside the HTTP session are per-servlet by design.

You could in principle override
VaadinServlet.createServletService
with a custom version returning a
VaadinServletService
whose
getServiceName
returns something unique but shared between your servlets. However, the principal method of having multiple UIs in an application is to write a custom
UIProvider
. This means you can’t configure the application using
@VaadinServletConfiguration
, though. Finally, if you don’t actually need multiple UIs but just multiple views with their own URLs, you might want to use the
Navigator
functionality instead.

Thx for your quick answer.

I probably misunderstood this sentence in the documentation :

“Use of multiple windows in an application may require defining and providing different UIs for the different windows. The UIs of an application share the same user session, that is, the VaadinSession object”

By now i’m not sure about choosing multiple views (and navigator) and multiple UIs. So i’m making some tries.

Let’s imagine we want an application that mimic outlook. Double-clicking a message in the list (main ui) would open a new (external) window, just to let us open multiple messages at the same time. Should we go for multiple UIs or multiple view ? Which one would be the best design and why ?

Regards

Using multiple UI:s created by one common UIProvider, can one use @Push in one of the UI:s only or will there be problems? (Haven’t tried it out)