Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

When will tomcat respond to the Datagram?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "when the tomcat will achieve the response Datagram", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "when will tomcat achieve the response Datagram?"

The emergence of doubt

This question arises when I am writing and downloading files. I use HttpServletResponse to get Outputstream, and then use OutputStream to write data directly. At that time, I wondered if this OutputStream was the OutputStream of the corresponding Socket connection. That is, when the program is written in stream, the data is also sent at the same time?

Where does Response's OutputStream write the data?

So I looked at HttpServletResponse's getOutputStream method to see what its annotations said.

/ * Returns a {@ link ServletOutputStream} suitable for writing binary * data in the response. The servlet container does not encode the * binary data. * *

Calling flush () on the ServletOutputStream commits the response. * * Either this method or {@ link # getWriter} may * be called to write the body, not both, except when {@ link # reset} * has been called. * * @ return a {@ link ServletOutputStream} for writing binary data * @ exception IllegalStateException if the getWriter method * has been called on this response * * @ exception IOException if an input or output exception occurred * * @ see # getWriter * @ see # reset * / public ServletOutputStream getOutputStream () throws IOException

Above, it is stated that OutputStream is used to write response body content, and the flush () method is also mentioned, indicating that there must be buffering, so it should not directly manipulate socket write data. My guess is that there should be a byte array for temporary storage and then unifying the flush. But I'm still not sure, so I simply flipped through the tomcat source code.

Find the implementation class CoyoteOuputStream for ServletOutputStream. It implements OutputStream's abstract method write, which writes data to fields of type OutputBuffer and stores them. And this OutputBuffer object comes from coyote/Response. In fact, this OutputBuffer is only an interface, the specific implementation has been turned down is StreamOutputBuffer. There is no limit to the size of the data and is stored in linked lists, with each linked list node storing 8196 bytes.

When will the response Datagram be returned to the client?

It's just to see when it calls the flush method of OutputBuffer. I looked at it layer by layer and finally located the finishResponse () method of connector/Response. This method first sends the response line and the response header. The response body is then sent. I haven't seen much of the source code of Tomcat. I found a good timing diagram here, which describes the processing of a HTTP request. Below, we focus on servlet's service method call and Response's fininshResponse method call. As you can see, after the service method returns, the finishResponse operation is performed. That is, when the servlet program processes the request, tomcat will send the response result back to the client

Note: servlet programs do not participate in the sending and receiving of the underlying data, or do not control

Where is the service method call for servlet in the figure?

Included in the internalDoFilter method of ApplicationFilterChain.

What does the servlet program mean by processing requests?

Basically, what the servlet program does is fill in the Request information based on the Response information.

What is the relationship between servlet programs and Spring MVC?

The underlying layer of Spring MVC is still Serlvet, which processes all requests with a servlet called DispatcherServlet, which in turn distributes the requests to the corresponding @ RequestMapping tagged method for processing. The whole thing is to complete a call to the service method.

What about the return page of MVC and the return of REST data?

When you return to the page, you write the page data to the response Body; the @ ResponseBody annotation actually writes the return value of the method annotated by @ RequestMapping into a JSON string into the response Body. The response Body here refers to OutputBuffer.

Responsibilities of Tomcat and Servlet programs

As mentioned in "How Tomcat works", there are three tasks for a Servlet container (Tomcat is a Servlet container).

1. Create a Request object and fill in the relevant information (parameters, headers, cookie, uri, etc.)

two。 Create a Response object

3. Call the service method of the Servlet associated with this request, passing Request and Response to it.

Let me say this in my own words: when the browser sends a request to the server, the server parses the contents of the request Datagram, creates a Request object filled with the request information, creates an "empty" Response object, and then passes these two objects to the servlet's service method to complete the filling of the Response object, and finally sends the Response data to the client.

Why do you want to pass Request objects?

If you don't pass the Request object, the Servlet program won't know what to populate. In other words, it doesn't know what kind of resources you want.

How does Tomcat find the Servlet associated with the request?

We know that when Tomcat develops, it is impossible to know what project you will deploy into it and what the servlet program is called. So it can't be hard-coded to call the service method, it uses the reflection mechanism.

Think about how we deployed the project before using the spring boot framework for development. Just package the project and put it in the webapp directory of Tomcat. After running, the corresponding URL of the project is localhost:8080/projectName/xxx, right? Moreover, in the project, whether annotated or web.xml, the mapping of the Servlet program will be configured. Map URL to a Servlet class file.

When the request comes, first find the corresponding project according to projectName, and then map to the corresponding Servlet class name according to the subsequent URL. Tomcat then uses the reflection mechanism to load the Servlet class file, get the instance, and then call the service method.

What is the relationship among coyote/Response, connector/Response and connector/ResponseFacade?

Coyote/Response is mainly linked to the underlying data transmission, while connector/Response is the upper wrapper of coyote/Response, which implements the HttpServletResponse interface. But if you pass it directly to the service method, you are afraid that the user will force the HttpServletResponse to connector/Response directly and call some of the underlying methods directly. So a "Facade mode" is introduced to block connector/Response except for the public methods defined by the HttpServletResponse interface. That is, what is passed to service is actually a connector/ResponseFacade object, and even if it is strongly converted to the actual type, you can only see the methods defined by the HttpServletResponse interface.

These are all the contents of the article "when will tomcat respond to the Datagram?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report