HttpServletRequest reuse

喜你入骨 提交于 2019-12-04 03:31:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!