2
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.

Community
  • 1
  • 1
Aj Otto
  • 124
  • 1
  • 16
  • the downvote is probably from a Google that led to the docs page: https://vaadin.com/wiki/-/wiki/main/setting+and+reading+session+attributes which is oudated and does not shed light to me. – Aj Otto Jun 20 '16 at 14:27
  • 1
    Your Vaadin UI class is session scoped, i.e. (usually) one UI for each user session. So, usually you put session scoped variables directly or indirectly into UI class. If you are familar with Spring framework, you could use a @UIScoped bean. No need to access the servlet session directly, I think. – Steffen Harbich Jun 20 '16 at 14:45
  • I have a JPA backend so I will look into creating a similar @UIScoped bean for this, thank you for the hint! – Aj Otto Jun 20 '16 at 14:49
  • 1
    Your wiki link is stating that it might be out dated. But it's not. Regarding spring, read [this](https://vaadin.com/wiki/-/wiki/Main/Vaadin+Spring). – Steffen Harbich Jun 20 '16 at 14:51
  • Woah, Spring can be used with JPA? I have been avoiding Spring as I though it would mean moving away from JPA. – Aj Otto Jun 20 '16 at 15:00
  • 1
    Oh, my link is regarding Spring and Vaadin. But you can use Spring in conjunction with JPA, see spring data. – Steffen Harbich Jun 20 '16 at 16:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115170/discussion-between-aj-otto-and-steffen-harbich). – Aj Otto Jun 21 '16 at 06:27

1 Answers1

3

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

HttpServletRequest request = (HttpServletRequest) VaadinService.getCurrentRequest();
request.getSession().setAttribute("name", "actual nameson");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Wijaya
  • 80
  • 6
  • now i'm gonna read up on these functions and how the sessions are scoped, thank you so much, [Wijaya](http://stackoverflow.com/users/4306188/wijaya) – Aj Otto Jun 21 '16 at 06:08
  • 1
    Another way to access the http session: VaadinSession.getCurrent().getSession(); – d2k2 Jun 21 '16 at 08:10
  • You can also use `VaadinServletService.getCurrentServletRequest().getSession()`. – Micer Nov 30 '16 at 10:27