Search code examples
javagwtjettycdiweld

Weld injected HttpSession into the session scoped bean is null


I'm running GWT app on Jetty 6.1 with Weld 2.0. Got the next code:

    @SessionScoped
    public class SessionContext implements Serializable {

        @Inject
        private HttpSession httpSession;

        public SessionContext() {
            super();
            //at this point httpSession is null
        }
    }

What am I missing, why HttpSession is not injected? Reference says that Injecting the HttpSession will force the session to be created.


Solution

  • Change the definition of

    public SessionContext() {
            super();
            //at this point httpSession is null
    }
    

    to

    public SessionContext(HttpSession httpSession) {
            super();
            this.httpSession = httpSession;
            //check session here
    }
    

    Also use constructor injection

    Otherwise provide a setter method for httpSession