Browser-server-request and response, browser response
When a browser accesses a server, it requests resources in essence.
For example, to request a static resource: index.html, enter www.a.com/index.html in the address bar of the browser. In order to support the HTTP protocol, the sent data must conform to the format of the HTTP protocol data, this is also the case:
GET/index.html HTTP/1.1 Accept-Language: zh-cn Connection: Keep-Alive Host: www.a.com User-Agent: Ila ila/4.0 (compatible; MSIE5.01; Window NT5.0) Accept-Encoding: gzip, deflate
This information becomes the request information, and the browser uses the Socket to send the network IO stream to the server. For a metaphor, the browser is like a boy, and the server is like a girl. What I want to express is that the browser actively links and actively requests data, that is, the desired resource, if the server has resources, she will only wait for the browser to actively request her to respond.
At this time, the server must be enabled, because she needs to listen to a port to know that someone has sent a message to her, for example, port 80. The server listens to port 80, which is similar to event-driven programming. The server uses ServerSocket to obtain request information. The server parses the request information to obtain the requested resource. Here we will simply split it into two parts:
1. Request static resources:
Upload file. If I/O Stream is used, load the file into the memory, set the status code to 200, output the file to the network I/O stream, and send it to the browser. The browser receives the response, captures the response body and uses the rendering engine (so-called kernel) for rendering. If the File cannot be Found, the server sets the response header to 404. We can customize a sentence to illustrate the cause. Most servers return 404 File Not Found. This is the process for loading static files.
2. Request dynamic resource servlet:
First, let's talk about the dynamic language, that is, the type of the variable can be known at runtime. Dynamic resources are unexecuted. class files, which obtain the browser request information. The server executes a series of pre-defined. class files and returns a dynamic result to the browser.
Just like static resources, if you want to buy an ordinary car, you can directly go to the car store and send a request to the car store. The car store will get a request and parse it, know which car you want to buy and return it to your car ). Dynamic resources, that is, you want to customize a luxury car, then you go to the car factory, send a request to the car factory, the car factory after receiving the request, parse it, get some useful information: car size, there are no ready-made vehicles to return directly. Instead, they must be dynamically generated through a series of production workshops. After the production is complete, the custom cars are returned and the connection is closed.
Let's start with the normal process. The server parses the request information sent by the browser and obtains the class name to be executed. Here we use [reflection]. I personally feel that the reflection technology is great. Although it does reduce performance, it reduces the coupling between classes, just like dynamic resources, I don't know how we can get dynamic resources without reflection. Get the desired class name, use reflection to get the class object, execute the desired method, and then return the result.
Note: This article is intended for Java Application Server Tomcat. As for the request and response principles of other servers, I did not make any research, so I did not give it a guess.
09:02:16
If you want to wear a crown, you must bear it again.