HttpServletRequest reuse
Asked Answered
L

1

6

It seems that some servlet containers reuse HttpServletRequest (or more generally, ServletRequest) instances between requests.

Question:

Can someone point to the servlet spec where this behavior (or the validity rules for references to such instances) is defined?

Lamont answered 2/9, 2014 at 15:23 Comment(0)
A
9

It is not defined in the Servlet API. It is an implementation detail.

In 3.11 for request objects (and 5.6 for response objects)

Each request object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method, unless the asynchronous processing is enabled for the component and the startAsync method is invoked on the request object. In the case where asynchronous processing occurs, the request object remains valid until complete is invoked on the AsyncContext. Containers commonly recycle request objects in order to avoid the performance overhead of request object creation. The developer must be aware that maintaining references to request objects for which startAsync has not been called outside the scope described above is not recommended as it may have indeterminate results

It is not required, but commonly used.

What is in the spec (see chapter 2.3.3) is the single threaded model. One request, one thread. This allows the request to be cleaned up and reused.

Antihistamine answered 2/9, 2014 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.