Announcement Announcement Module
No announcement yet.
Question on Session Scope Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on Session Scope

    Does anyone know, when I define a bean to be session scoped, would that implicitly create a session (getSession(true)) even if no session existed at the given time?

    In other words, I want a bean to be request scoped if no session is available, and move that bean to session scope whenever the application decides to create a session (e.g. after a user login).

    Background: I don't want the application to have a user session by default. A loggedInUserCredentials bean would however be injected to the controller - being initialized as "anonymous" from scratch. When a user logs in the app will create a session (getSession(true)), and from that point on the loggedInUserCredentials bean should reside in session scope.

    Any tricks or legal methods to accomplish this?

    Thank you all, br,

  • #2
    Session scope is called session scope for a reason . It requires a session to be active.

    Also from your short description it seems to me that you want to simply store something in the session, the user object (or credentials) why not simply do it yourself and retrieve it where needed?

    Basically if you want the behavior as described you would need to implement yuor own scope (or modify the existing scope)
    Last edited by Marten Deinum; Mar 19th, 2010, 05:51 AM.


    • #3
      Thanks for your answer Marten.
      Actually what I want to achieve is to have a "generic" user credentials bean injected into the controller that is either request based (if no user session is available) or session based (if a user session exists).

      I believe I could more easily handle that using a custom BeanFactory which would have request scope? This factory could then look if a session is available and inject the bean from there if it exists, or create and store a request scoped bean. By any means this would effectively hide the logic from the controller code (thats what I intend).

      Only issue here - at request start the factory finds there is no session, so it creates a request-scoped bean. Later during handling of the request the application decides to open a session - from this moment on the bean would need to be located in session storage. How could the BeanFactory become aware of the fact that now a session is available?

      Thanks for insights,