First understand the basic concepts: what is stateless and what is a non-connected stateless protocol:
- The state of the Protocol refers to the ability of the next transmission to "remember" the transmission of this information.
- HTTP is not to maintain the information transmitted by this connection for the next connection, in order to ensure server memory.
- For example, when a customer gets a Web page, closes the browser, launches the browser again, and then logs on to the site, but the server does not know that the client closed the browser.
- Due to the Web server's concurrent access to many browsers, in order to improve the processing power of the Web server for concurrent access, when designing the HTTP protocol, the Web server is required to send HTTP response messages and documents without saving any state information from the Web browser process that made the request. It is possible for a browser to access the same object two times within a few seconds, and the server process will not accept a second service request because it has already sent a reply message to it. Because the Web server does not save any information about the Web browser process that sent the request, the HTTP protocol is a stateless protocol (stateless Protocol).
The HTTP protocol is a stateless and connection:keep-alive difference:
- Stateless means that the protocol has no memory capacity for transactions, and the server does not know what the client state is. On the other hand, there is no connection between opening a Web page on a server and the pages you have previously opened on this server.
HTTP
is a stateless connection-oriented protocol, stateless does not mean that HTTP
the connection cannot be maintained TCP
, and it cannot represent a HTTP
UDP
protocol (no connection).
- From
HTTP/1.1
the beginning, the default is to open the keep-alive, to maintain the connection characteristics, in short, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection is not closed, if the client again access to the Web page on this server, will continue to use this established connection.
Keep-Alive
Does not permanently keep the connection, it has a hold time and can be set in different server software (such as Apache) for this time.
HTTP is a stateless protocol, which means that each request is separate, keep-alive (after the server finishes processing the client's request and receives the client's answer, it does not immediately disconnect), the result cannot be changed.
Over time, however, people find static HTML boring and tedious, adding dynamically generated content to make Web applications more useful. Thus, the syntax of HTML is expanding, the most important thing is to increase the form (form), the client also added such as script processing, DOM processing and other functions, for the server, the corresponding CGI (Common Gateway Interface) to handle dynamic requests that include form submissions.
After the advent of a Web application in which the client interacts dynamically with the server, the HTTP stateless feature seriously hinders the implementation of these interactive applications, after all, the simple shopping cart program also needs to know what product the user has chosen before. As a result, two techniques for keeping the HTTP state are created, one is a cookie and the other is a session.
A cookie is a client's storage space, maintained by a browser . In particular, the cookie mechanism uses a scheme that maintains state on the client, while the session mechanism uses a scenario that maintains state on the server side . At the same time, we also see that because the server-side hold-state scheme also needs to save an identity on the client side, the session mechanism may need to use the cookie mechanism to achieve the purpose of preserving the identity, but there are actually other options, such as rewriting the URL and hiding the form field.
Simply put, cookies and sessions serve to preserve the state of the client, but they do not change the stateless nature of the HTTP protocol itself, which can be understood as a state reservation on the application.
Why does the HTTP protocol be stateless?