HTTP protocol is stateless protocol, how to understand? Session Cookie

Source: Internet
Author: User
Tags http request session id

HTTP is a stateless protocol, with two consecutive requests for the same session that are not known to each other, and they are parsed by the newly instantiated environment, which does not save any information about the session, except that the application itself may already be stored in the global object.

Own understanding, In asp: Every time the page is committed, the next commit will not be remembered, except that the application itself may have been stored in the global object. NET is actually viewstate,viewstate is the basic form of saving page information in ASP. BITS, which are stored in the hidden fields of the control, etc.

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.

Stateless means when the browser sends a request to the server, the server responds, but the same browser sends the request to the server, he will respond, but he does not know that you are just that browser, simply said that the server will not remember you, so is a stateless protocol.
DNS is a stateful protocol.

HTTP is an object-oriented protocol belonging to the application layer, the HTTP protocol has five major features, 1, support client/server mode, 2, simple and fast, 3, flexible, 4, no connection, 5, stateless; "Stateless" is one of the main features of the HTTP protocol, and the following is a "stateless" explanation. No status: Refers to the

The protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.

HTTP is the abbreviation for Hyper Text Transfer protocol, which, as its name implies, supports the transmission of hypertext. So what is hypertext? To be blunt is to use HTML to write pages. Typically, we use the client browser to access the server's resources, and the most common URLs are files that are suffixed with HTML. Therefore, we can say that hypertext is the most important resource on the network.

Since the purpose of the HTTP protocol is to support the transmission of hypertext, more broadly, is to support the transfer of resources, then the client browser to the HTTP server to send a request, and then the HTTP server sends the appropriate resources back to the client, whether for the client or the server, There is no need to document this process, because each request and response is relatively independent, as if you were dropping a coin in front of the vending machine to buy a product, and no one would need to remember such a trading process. In general, a URL corresponds to a unique hypertext, and the HTTP server is absolutely fair, regardless of whether you are Michael or Jordon, it will return the same hypertext based on the URL request received. It is this uniqueness that makes it meaningless to record the behavior of the user, so the HTTP protocol is designed as a stateless connection protocol that meets its own needs.

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 applications, after all the interaction needs to be followed, and the simple shopping cart program knows what the user has chosen before. As a result, two techniques for keeping the HTTP connection state are created, one is a cookie and the other is a session.

A cookie is a solution that maintains state through the client. By definition, a cookie is a special message that is sent to the client by the server, which is stored as a text file on the client, and then each time the client sends a request to the server, the special information is brought. Let's be more specific: When a user uses a browser to access a Web site that supports cookies, the user provides personal information including the user's name and submits it to the server, and the server sends back the personal information when it sends the corresponding hypertext to the client. Of course, this information is not stored in the HTTP response body (Response body), but is stored in the HTTP response header (Response header), when the client browser receives a response from the server, the browser will store this information in a unified location, For the Windows operating system, we can find the stored cookie from: [System disk]:\documents and settings\[user name]\cookies directory, and since then, when the client sends the request to the server, will send the corresponding cookie back to the server again. This time, the cookie information is stored in the HTTP request header.
With the implementation of a technology such as cookies, when the server receives a request from the client's browser, it is able to generate the client-specific information by analyzing the cookie stored in the request header, which dynamically generates the content corresponding to that client. Usually, we can see the "Please remember Me" option from the login screen of many websites, if you check it and then log in, then the next time you visit the site will not need to repeat the cumbersome login action, and this feature is implemented through a cookie.

A solution that is relative to a cookie is the session, which is maintained by the server. Since the term session contains a lot of semantics, it is necessary to clarify the meaning of the session here. First, we usually translate sessions into conversations, so we can refer to a series of interactions between the client browser and the server as a session. From this semantics, we will refer to the duration of the session, what is done during the session and so on, and second, the session refers to the server side for the client to open up the storage space, in which the information is used to hold the state. From this semantics, we will refer to what is stored in the session, how to get the matching content from the session according to the key value.
To use the session, the first step is of course to create a session. So when is the session created? Of course, it is created in the process of running the server-side program, the different language implementation of the application has different methods to create the session, and in Java by calling HttpServletRequest's GetSession method (using True as a parameter) created. When the session is created, the server generates a unique session ID for the session, and the session ID is used to regain the session that was created in the subsequent request, and after the session is created, You can call the session related methods to add content to the session, which will only be saved in the server, sent to the client only session ID, when the client sends the request again, the session ID will be taken, Once the server accepts the request, it will find the corresponding session based on the session ID, which is used again. Formally, the state of the user is maintained. The content of the session is also more, in the future post, I will continue to tell.

        in summary, HTTP itself is a stateless connection protocol, in order to support the interaction between the client and server, we need to use different technologies for the interactive storage State, And these different technologies are the cookies and the session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.