users@jersey.java.net

Re: [Jersey] setting session attributes at resource startup

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Mon, 30 Jun 2008 08:04:10 -0600

Paul,

I get this exception now.

Caused by: java.lang.InstantiationException: resources.MyResource
        at java.lang.Class.newInstance0(Class.java:335)
        at java.lang.Class.newInstance(Class.java:303)
        at
com.sun.ws.rest.impl.application.WebApplicationImpl$DefaultComponentProvider.getInstance(WebApplicationImpl.java:293)
        at
com.sun.ws.rest.impl.resource.SingletonProvider.init(SingletonProvider.java:44)
        ... 50 more
2008-06-30 07:57:32.584::WARN: Nested in
javax.servlet.ServletException:
com.sun.ws.rest.api.container.ContainerException: Unable to create resource:
com.sun.ws.rest.api.container.ContainerException: Unable to create resource
        at
com.sun.ws.rest.impl.resource.SingletonProvider.init(SingletonProvider.java:46)


When I remove @Singleton annotation from my resource, this exception
goes away. But, now I still get the NPE.

Thank you,
Arul

Paul Sandoz wrote:
> 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.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> ________________________________
> Scanned by MessageLabs for Flux
> ________________________________