4

I would like to understand the full meaning of the @SessionScoped annotation in a JSF ManagedBean.

I guess it is related to the http session, but still, it's not very clear to me when it starts, when it ends and if it can be interrupted.

  • If I leave a browser open, without activity, during 2 hours. Is the session still open?
  • Is a session shared across browser tabs
  • Does a session behave identical in say Firefox, IE or Safari?
  • ...
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Jan
  • 9,397
  • 13
  • 47
  • 52
  • JSF runs on top of Servlet API. JSF session is indeed backed by HttpSession. I think it's time to learn Servlet API as well. You may find [this answer](http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909) helpful as well. – BalusC Aug 04 '10 at 14:37

1 Answers1

3
  1. no, the session times-out if there is no activity
  2. yes, the session is shared across browser tabs
  3. yes, the session is a server-side notion mainly, browsers only send a cookie to identify

The session starts when the user requests the first page.

The @PostConstruct method of session scoped beans (if exists) is invoked when you first access a page that references that bean (I'm not entirely sure in this, though)

The session ends when you call session.invalidate() or when it times-out (the timeout period if configurable in web.xml). Then the @PreDestroy method (if exists) is invoked.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • In fact, here you have the javadoc: http://docs.jboss.org/cdi/api/1.0/javax/enterprise/context/SessionScoped.html – Herme Nov 17 '11 at 23:05
  • Was looking for information on when @SessionScoped bean instances are created. Indeed, you're right. They're not created automatically when a session starts, but when they're first referenced by a page or injected in a lower scope bean that is used by a page. Thought my tests were wrong but you're assumption is some kind of confirmation. – Marcel N. May 06 '13 at 22:26