Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest reuse

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?

like image 272
MRalwasser Avatar asked Sep 02 '14 15:09

MRalwasser


People also ask

What is the difference between HttpServletRequest and HttpServletResponse?

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."

Why do we use HttpServletRequest?

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.

What is ServletRequest and ServletResponse?

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.


1 Answers

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.

like image 134
Sotirios Delimanolis Avatar answered Sep 23 '22 02:09

Sotirios Delimanolis