问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/25626986/httpservletrequest-reuse