Hi Paul,
Thanks for your help.
I tried this code in both 0.7 and 0.8.
I get NPE at r.getPathInfo() when I GET "perrequest/constructor" in
Jersey 0.7. All three cases work like a charm in 0.8.
Something like shown below won't work even in 0.8?
@Path("singleton")
@Singleton
public static class SingletonResource {
@Context
HttpServletRequest r;
public SingletonResource(@Context HttpServletRequest r) {
this.r = r;
r.getSession().setAttribute("test", "true");
}
@ProduceMime("text/plain")
@GET
public String get() {
//return r.getPathInfo();
return (String)r.getAttribute("test");
}
}
Thank you,
Arul
Paul Sandoz wrote:
> Hi,
>
> Ah, i did not realize you resource class was a singleton. The more
> information/code you per-email send the faster i can help you :-)
>
> This is working fine for me in 0.8, see example below. Does that
> example work for you?
>
> Paul.
>
> public class Inject {
> @Path("singleton")
> @Singleton
> public static class SingletonResource {
> @Context HttpServletRequest r;
>
> @ProduceMime("text/plain")
> @GET public String get() {
> return r.getPathInfo();
> }
> }
>
> @Path("perrequest")
> public static class PerRequestResource {
> @Context HttpServletRequest r;
>
> @ProduceMime("text/plain")
> @GET public String get() {
> return r.getPathInfo();
> }
> }
>
> @Path("perrequest/constructor")
> public static class PerRequestConstructorResource {
> String path;
>
> public PerRequestConstructorResource(@Context
> HttpServletRequest r) {
> path = r.getPathInfo();
> }
>
> @ProduceMime("text/plain")
> @GET public String get() {
> return path;
> }
> }
> }
>
> Arul Dhesiaseelan wrote:
>> 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
>>> ________________________________
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>