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?
HttpServletRequest "extends the ServletRequest interface to provide request information for HTTP servlets." HttpServletResponse "extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies."
Interface HttpServletRequest. Extends the ServletRequest interface to provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods ( doGet , doPost , etc). String identifier for Basic authentication.
ServletRequest and ServletResponse are two interfaces that serve as the backbone of servlet technology implementation. They belong to the javax. servlet package. Signature: public interface ServletRequest. Blueprint of an object to provide client request information to a servlet.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With