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

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

问题描述

我正在研究用于 Web 的 Java,它提到 http 是无状态的.这是什么意思以及它如何影响编程

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

我也在研究 spring 框架,它提到一些 bean 随着它们的状态变化必须声明为内部 bean.这是什么意思?

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——即服务器和客户端之间的实际传输协议——是无状态的",因为它在调用之间不记得任何事情.每个通过 HTTP 访问的资源都是一个请求,它们之间没有线程连接.如果您加载一个包含三个 <img> 标签的 HTML 文件访问同一服务器的网页,将协商并打开四个 TCP 连接,四个数据传输,四个连接关闭.在服务器协议级别中,根本没有状态可以让服务器在您进入时了解您的任何信息.

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.

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

(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.)

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

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.

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

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.

可是……

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

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天全站免登陆