HTTP protocol stateless, exactly how to understand?

A story,

One day, you have a demand, you need to go to the supermarket to buy a bottle of soy sauce. Go to the supermarket to buy soy sauce, you tell the salesman, next time prepare flour for me, I will take it next time. The second time, you go to the supermarket to get the flour, and the clerk says he can’t remember when you said to prepare the flour. This time you learn to be smart, the salesman wrote you a note, above the supermarket seal, next time you bring the note, buy the supermarket to prepare for you the flour

Stateless protocol

  1. HTTP: Hyper Text Transfer Protocol

  2. HTTP does not maintain this connection for the information needed for the next connection

  3. Just like when you go to the supermarket to buy soy sauce, it’s over, it doesn’t record what you told him, it doesn’t record that you’re going to buy flour next time, and the next time he doesn’t know you’ve been there

  4. As the name implies, stateless protocol means that when the browser sends a request to the server, the server responds, but when the same browser sends a request to the server again, it responds, but it does not know that you are the same browser. Simply put, the server does not remember you, so it is stateless protocol. DNS is a stateful protocol.

  5. Another example is a shopping cart, where you buy something and add it to the shopping cart, and if HTTP is used, refresh the page and the shopping cart is empty.

Cookies and sessions

  1. The stateless nature of HTTP is a serious impediment to the emergence of Web applications that interact dynamically between client and server. After all, the interaction needs to be connected, and a simple shopping cart program needs to know exactly what the user has selected. As a result, there are two techniques for maintaining HTTP connections: cookies and sessions.

  2. Cookies are a solution for preserving state through clients. By definition, a Cookie is a special message sent by the server to the client, which is stored as a text file in the client. The client then carries the special message with it each time it sends a request to the server. Let’s be more specific: when a user visits a cookie-enabled site using a browser, the user provides personal information including username and submits it to the server. Then, when the server sends the corresponding hypertext back to the client, the personal information is also sent back. Of course, the personal information is stored in the HTTP Response Header instead of the HTTP Response Body.

    To put it simply, the cookie is saved on the client side. It is similar to when you go to the supermarket, the salesman gives you a slip of paper, and you take the slip away. Next time you come back, bring the slip with you

  3. An alternative to cookies is the Session, which maintains state through the server. Because the term Session contains many semantics, it is necessary to understand the meaning of Session here. First, we usually translate sessions as sessions, so we can call a Session a series of interactions between the client browser and the server. So from this semantics, we’re going to talk about how long the Session lasts, we’re going to talk about what happens during the Session, etc.; Second, Session refers to the storage space opened by the server for the client, and the information stored in it is used to maintain the state. From this point of view, we’ll talk about what to store in the Session and how to retrieve matches from the Session based on key values.

    To put it simply, the session is saved on the server side, just like when you go to the supermarket, the salesman will write down on his slip what John will buy next time, and next time you come to tell him your name is John.

    reference