0

I am running the code given in answer to this question-

Servlet-3 Async Context, how to do asynchronous writes?

Instead of response.getWriter().write(some_big_data); I've changed that line to

 ServletRequest req= ctx.getRequest();

 response.getWriter().write(req.getContentType());

Now, request is just timing out. How can I access request object?

Community
  • 1
  • 1
user375868
  • 1,288
  • 4
  • 21
  • 45

1 Answers1

0

I'm assuming you are having problems using that code snippet from within your own thread after a startAsync() call.

Per the Servlet 3.0 spec, section 2.3.3.4, access to the Request and Response objects are not thread-safe. In fact, depending on the state of the object lifecycle, the request and response objects can even be recycled.

It is encouraged that you grab what you need from the Request and Response objects before you startAsync() and use those references from your own thread.

In other words, your use of ctx.getRequest() and response.getWriter() should be done before you startAsync()

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136