users@jersey.java.net

[Jersey] Re: Access HttpServletRequest and HttpServletResponse in MessageBodyWriter

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 19 Sep 2014 19:29:58 +0200

You should be able to just inject the HttpServletRequest/Response directly. There's no need to use ThreadLocal, the proxy will be created automatically by Jersey/HK2.

Marek

On 19 Sep 2014, at 18:48, Kevin Zhang <zhangyongjiang_at_yahoo.com> wrote:

> Hi,
>
>
>
> I'm farmiliar with Jersey 1. I'm now switching to Jersey 2 and got some problem with my implementation of MessageBodyWriter.
>
> I used code below to access the HttpServletRequest and HttpServletResponse
>
> @Context
> private ThreadLocal<HttpServletRequest> requesThreadLocal;
>
> @Context
> private ThreadLocal<HttpServletResponse> responseThreadLocal;
>
> It worked before with Jersey 1 but now I got NullPointerException.
>
> Specifically, I need to access the HttpServletRequest so that I can call the setAttribute and put beans in the HttpServletRequest object. The beans later will be used by JSP pages. Is there any other alternative?
>
> Your valuable input is hightly appreciated.
>
> Kevin
>
>
>