当他们说http是无国籍时,这是什么意思 [英] what does it mean when they say http is stateless

查看:176
本文介绍了当他们说http是无国籍时,这是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究java for web,它提到http是无状态的。
这意味着什么以及它如何影响编程



我也在研究弹簧框架,并提到一些豆子必须宣称为内豆作为他们的内豆国家变化。这意味着什么?

解决方案

HTTP--这是服务器和客户端之间的实际传输协议 - 是无状态的 因为它在调用之间没有记住任何东西。 通过HTTP访问的每个资源都是单个请求,它们之间没有线程连接。如果您加载一个网页,其中包含一个HTML文件,其中包含三个< img> 标记,这些标记会在同一个服务器上进行协商,那么将会有四个TCP连接协商并打开,四个数据转让,四个连接关闭。服务器处于协议级别根本没有保留状态,这将使服务器在您进入时了解您的任何信息。



(嗯,无论如何,对于高达1.0的HTTP都是如此.HTTP 1.1增加了各种类型的持久连接机制,因为真正的无状态协议产生了不可避免的性能问题。我们暂时忽略了这一点。因为它们并没有真正使HTTP成为有状态,它们只是使它变为无状态而不是纯无状态。)



为了帮助您理解差异,想象一下协议像Telnet或SSH是无国籍的。如果您想获得远程文件的目录列表,则必须作为一个原子操作连接,登录,更改到目录并发出 ls 命令。当 ls 命令完成显示目录内容时,连接将关闭。如果您想显示特定文件的内容,则必须再次连接,登录,切换到目录,然后发出 cat 命令。当显示文件的命令完成后,连接将再次关闭。



当你这样看时,虽然是Telnet / SSH的镜头,听起来很愚蠢,不是吗?嗯,在某些方面它是,在某些方面它不是。当协议是无状态时,服务器可以做一些非常好的优化,并且数据可以很容易地传播。使用无状态协议的服务器可以非常有效地扩展,因此实际的单个数据传输速度非常慢(打开和关闭TCP连接并不便宜!)整个系统可以非常非常高效,并且可以扩展到任意数量的用户。 / p>

但是......



除了查看静态网页之外,您几乎要做的任何事情都涉及会话和状态。当HTTP用于其原始目的(共享静态信息,如科学论文)时,无状态协议非常有意义。当您开始将它用于Web应用程序,在线商店等时,那么无国籍状态开始变得麻烦,因为这些本身就是有状态的活动。结果,人们很快想出了在无状态协议之上处理国家的方法。这些机制包括诸如cookie之类的东西,比如URL中的编码状态,以及让服务器根据这些来动态启动数据,比如隐藏的状态请求,比如...好吧,像一大堆东西,包括更现代的Web套接字之类的东西。



以下是一些链接,您可以按照这些链接深入理解这些概念:




I am studing java for web and it mentions http is stateless. what does that mean and how it effects the programming

I was also studying the spring framework and there it mentions some beans have to declared as inner beans as their state changes . What does that means?

解决方案

HTTP -- that is the actual transport protocol between the server and the client -- is "stateless" because it remembers nothing between invocations. EVERY resource that is accessed via HTTP is a single request with no threaded connection between them. If you load a web page with an HTML file that within it contains three <img> tags hitting the same server, there will be four TCP connections negotiated and opened, four data transfers, four connections closed. There is simply no state kept at the server at the protocol level that will have the server know anything about you as you come in.

(Well, that's true for HTTP up to 1.0 at any rate. HTTP 1.1 adds persistent connection mechanisms of various sorts because of the inevitable performance problems that a truly stateless protocol engenders. We'll overlook this for the moment because they don't really make HTTP stateful, they just make it dirty-stateless instead of pure-stateless.)

To help you understand the difference, imagine that a protocol like Telnet or SSH were stateless. If you wanted to get a directory listing of a remote file, you would have to, as one atomic operation, connect, sign in, change to the directory and issue the ls command. When the ls command finished displaying the directory contents, the connection would close. If you then wanted to display the contents of a specific file you would have to again connect, sign in, change to the directory and now issue the cat command. When the command displaying the file finished, the connection would again close.

When you look at it that way, though the lens of Telnet/SSH, that sounds pretty stupid, doesn't it? Well, in some ways it is and in some ways it isn't. When a protocol is stateless, the server can do some pretty good optimizations and the data can be spread around easily. Servers using stateless protocols can scale very effectively, so while the actual individual data transfers can be very slow (opening and closing TCP connections is NOT cheap!) an overall system can be very, very efficient and can scale to any number of users.

But...

Almost anything you want to do other than viewing static web pages will involve sessions and states. When HTTP is used for its original purpose (sharing static information like scientific papers) the stateless protocol makes a lot of sense. When you start using it for things like web applications, online stores, etc. then statelessness starts to be a bother because these are inherently stateful activities. As a result people very rapidly came up with ways to slather state on top of the stateless protocol. These mechanisms have included things like cookies, like encoding state in the URLs and having the server dynamically fire up data based on those, like hidden state requests, like ... well, like a whole bunch of things up to and including the more modern things like Web Sockets.

Here are a few links you can follow to get a deeper understanding of the concepts:

这篇关于当他们说http是无国籍时,这是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆