users@jersey.java.net

Re: [Jersey] setting session attributes at resource startup

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 30 Jun 2008 10:13:22 +0200

On Jun 27, 2008, at 11:06 PM, Arul Dhesiaseelan wrote:

> Hi,
>
> I am trying to set up a session attributes in the constructor of
> the Resource. But, it looks like the session object is not injected
> yet when I try to set attribute on the session.
>
> @Context
> private HttpServletRequest httpRequest;
>
> public MyResource() {
> httpRequest.getSession().setAttribute("securityenabled",
> "true");//fails due to NPE
> }
>
>
> Is there any other way to setup session attributes in the resource
> at startup?
>

Injection on fields can only after construction. Try doing the
following instead:

  private HttpServletRequest httpRequest;

  public MyResource(@Context HttpServletRequest httpRequest) {
    this.httpRequest = httpRequest;
    httpRequest.getSession().setAttribute("securityenabled",
"true");//fails due to NPE
  }

Paul.