Michael
Michael

Reputation: 42100

Does servlet engine read the whole request before calling a servlet?

Servlet engine (e.g. Tomcat or Jetty) receives an HTTP request and calls a servlet with an HttpServletRequest object, which contains an InputStream of the request body.

Now I wonder if the engine has already read the whole request from the network and the InputStream is just a buffer in memory or it has read the request partially and when the servlet calls the InputStream.read it actually reads the socket.

Upvotes: 3

Views: 242

Answers (3)

firfor
firfor

Reputation: 169

you can read this article. where does the HttpServeltInputStream read from — Zhihu, this article is written by myself. It's written in Chinese, and if you don't read Chinese, here's a neat conclusion:

enter image description here

picture source: https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

you can also read some tomcat information from this question: What is "Sim blocking" (seen in tomcat doc)?

Actually, in tomcat HttpServletRequest.getInputStream will read data from NIOChannel(NIOChannel source code in github).

Although many buffer objects are used during the read. But these are still small buffers. Eventually it's a little bit of reading from the NIOChannel directly from the socket

enter image description here

  • We read stream byte from CoyoteInputStream directly.
  • It (CoyoteInputStream) copy or read byte stream from the InputBuffer ;
  • InputBuffer read data from the low level request Object: CoyoteRequest (which class is: org.apache.coyote.Request)
  • CoyoteRequest read data from Http11InputBuffer if we use Connector: HTTP/1.1
  • Http11InputBuffer read data from NIOSocketWrapper
  • NIOSocketWrapper read data from NIOChannel

Upvotes: 1

user207421
user207421

Reputation: 310980

It has to, at least in the case of POST, so it can form the requestParameterMap from the name-value pairs in the body of the request.

Upvotes: 1

ZhongYu
ZhongYu

Reputation: 19682

Usually that's not the case, because the request body can be really huge. A servlet container MAY do that if the content length is known and is small enough.

Upvotes: 1

Related Questions