HTTP is a stateless protocol. How can this problem be solved?

Source: Internet
Author: User

HTTP is a stateless protocol. Two consecutive requests in the same session do not know each other. They are parsed by the latest instantiated environment, except for all information that the application may have stored in a global object, the environment does not save any session-related information.

In Asp.net, there is no relationship between the pages submitted to the server each time. The information recorded on the page each time cannot be remembered for the next submission, (except for all information that the application may have stored in the Global Object) in. in net, viewstate is actually used. viewstate is the basic unit for saving page information in Asp.net, and data stored in the hidden fields of controls is used.

The status of the Protocol indicates the ability to "remember" The transfer information during the next transmission.
HTTP does not maintain the information transmitted by this connection for the next connection.

Stateless means that when a browser sends a request to the server, the server responds, but the same browser sends the request to the server again, it will respond, but he doesn't know that you are the browser just now. Simply put, the server won't remember you, so it's a stateless protocol.
While DNS is a stateful protocol.

HTTP is an object-oriented protocol at the application layer. HTTP has five main features: 1. Support for customer/Server models; 2. simple and fast; 3. flexible; 4. No connection; 5. stateless; "stateless" is one of the main features of HTTP. The following is an explanation of "stateless. Stateless: indicates

The Protocol has no memory for transaction processing. The lack of status means that if subsequent processing requires the previous information, it must be re-transmitted, which may increase the amount of data transmitted each connection. On the other hand, when the server does not need previous information, its response is faster.

HTTP is short for Hyper Text Transfer Protocol. As the name suggests, this protocol supports Hypertext Transfer. So what is hyper text? To put it bluntly, it is a page written in HTML. Generally, we use a client browser to access server resources. The most common URL is an HTML file. Therefore, hypertext is the most important resource on the network.

Since the HTTP protocol is designed to support Hypertext Transfer, and more broadly, it supports resource transmission, the client browser sends requests to the HTTP server, when the HTTP server sends the corresponding resources back to the client, there is no need to record this process for both the client and the server, because every request and response are relatively independent, just as if you cast a coin in front of a vending machine to buy a product, no one needs to remember this transaction process. Generally, a URL corresponds to a unique hypertext, And the HTTP server is absolutely fair. Whether you are Michael or Jordon, it will return the same hypertext according to the received URL request. This uniqueness makes it meaningless to record the user's behavior status. Therefore, the HTTP protocol is designed as a stateless connection protocol to meet its own needs.

However, with the passage of time, people found that static html is really boring and boring. Adding dynamic content will make Web applications more useful. As a result, the HTML syntax is constantly expanding. The most important of them is the addition of form. The client also adds functions such as script processing and Dom processing. for servers, the corresponding CGI (Common Gateway Interface) appears to process dynamic requests including form submission. After the emergence of such a web application that dynamically interacts with the server, the stateless HTTP feature severely hinders the implementation of these applications. After all, interaction must be carried forward, A simple Shopping Cart program also needs to know what items the user has selected before. Therefore, the two technologies used to maintain the HTTP connection status came into being. One is cookie, and the other is session.

Cookie is a solution that maintains the status through the client. By definition, cookies are the special information sent from the server to the client, which is stored on the client as text files, the client then carries the special information each time it sends a request to the server. Let's be more specific: when a user accesses a website that supports cookies through a browser, the user will provide personal information including the user name and submit it to the server. Then, the server will send back the corresponding hypertext to the client and the personal information. Of course, this information is not stored in the HTTP response body, it is stored in the HTTP Response Header. When the client browser receives a response from the server, the browser stores the information in a unified location. For Windows operating systems, we can find the stored cookie in the [System Disk]: \ Users and Settings \ [user name] \ cookies directory. Since then, when the client sends a request to the server, will send the corresponding cookie back to the server. This time, the cookie information is stored in the HTTP request header.
With the implementation of cookie technology, after receiving a request from a client browser, the server can analyze the cookie stored in the request header to obtain the unique information of the client, in this way, content corresponding to the client is dynamically generated. Generally, we can see the option "remember me" in the logon interface of many websites. If you select this option, log on again, therefore, the next time you access the website, you do not need to perform repeated and cumbersome logon actions. This function is implemented through cookies.

A solution opposite to Cookie is Session, which is maintained by the server. The session vocabulary contains a lot of semantics, so we need to clarify the meaning of the session here. First, we usually translate sessions into sessions, so we can call a series of interactions between the client browser and the server a session. Starting from this semantics, we will mention the session duration and the operations performed in the session process. Secondly, session refers to the storage space opened by the server for the client. The information stored in the session is used to maintain the status. Starting from this semantics, we will mention what content is stored in the session and how to obtain Matching content from the session based on the key value.
The first step is to create a session. When will the session be created? Of course, it was created during the running of the server program. applications implemented in different languages have different methods to create sessions, in Java, it is created by calling the getsession method of httpservletrequest (using true as the parameter. When a session is created, the server generates a unique session ID for the session, which will be used in subsequent requests to obtain the created session; after a session is created, you can call the session-related method to add content to the session. The content is only saved on the server and only the session ID is sent to the client; when the client sends the request again, the session ID will be carried. After the server receives the request, it will find the corresponding session based on the session ID and use it again. In this process, the user's status is maintained. There are still a lot of session-related content. I will continue to talk about it in future post.

To sum up, HTTP itself is a stateless connection protocol. to support interaction between the client and the server, we need to use different technologies to store the status of interaction, these different technologies are cookie and 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.