Checking if a session variable exists in Vaadin

1.3k Views Asked by At
VaadinSession.getCurrent().getAttribute("name")
&
VaadinSession.getCurrent().setAttribute("name", "actual nameson")

vs

getSession().getAttribute("name", username);
&
getSession().setAttribute("name", username);

are either of these correct methods to use Vaadin's Session management? I cannot find understandable documentation on getting and setting a session in Vaadin.

My current problem: if I try to Get a session variable, I get a nullpointerException, I tried to swallow it and continue but this makes Vaadin implode.

Is there a safe way to check if a Vaadin session (and specific variable) is set?

Do I have to initialize a session before I can use the variables?

more specifically, I want to check if a session exists before a user is redirected to the login screen. if the user is not logged in, the check throws a nullpointerException so a user who is not logged in cannot load the loginForm.

question @ How to get all sessions in Vaadin is not related to my problem.

one more thing

boolean isLoggedIn = getSession().getAttribute("user") != null;

does not make Vaadin implode, so I am probably just doing a Java Fail™

any help would be much appreciated.

1

There are 1 best solutions below

3
On BEST ANSWER

You can directly access the HTTP Session if needed, using VaadinService.

HttpServletRequest request = (HttpServletRequest) VaadinService.getCurrentRequest();
request.getSession().setAttribute("name", "actual nameson");