Question

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.

Was it helpful?

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

OTHER TIPS

It's better to use @PostConstruct to annotate some other method, here you have :

Initializing a managed bean specifies the lifecycle callback method that the CDI framework should call after dependency injection but before the class is put into service.

This is exactly the place where your injections are done but no code has been invoked.

like this :

@PostConstruct
public void doMyStuff() {
  //feel free to access your injections here they are not null!
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top