42

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?

kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 4
    Give us some reference to book/paper that you are studying. I don't understand why are you clubbing together the state of a networking protocol with a state of a object in a single question? Where we can find that information about Spring beans that you have mentioned? – Ritesh Feb 06 '11 at 14:38

5 Answers5

66

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:

JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
  • thanks for useful stuff , but suppose if http2.1 becomes stateful then how current scenario chnage with respect to web page display. i mean i want to see the diff now and then –  Feb 06 '11 at 14:47
  • 1.1, not 2.1. ;) And as I said, HTTP 1.1 doesn't really go stateful. It just has some optimizing tricks that allow you to reuse connections, etc. From the web browser perspective it means that your web sites load more quickly because you only have to negotiate the TCP connection once for those HTML file + 3 images (to use my example) instead of 4 times. – JUST MY correct OPINION Feb 06 '11 at 14:51
  • 1
    why can't they make it stateful . is there any programming issue or it can't be done –  Feb 06 '11 at 14:57
  • 3
    Inertia? I can't really read their minds. There's no technical reason preventing them from supporting stateful connections (given that people are slathering state on top of HTTP quite easily) and, indeed, the whole point of [Web Sockets](http://en.wikipedia.org/wiki/WebSockets) is to provide TCP-like connections (TCP is a stateful, connection-oriented low-level protocol) through HTTP. It's a mystery to me, personally, but I'm not a protocol designer. Maybe there's something about stateless protocols that's worth the pain. – JUST MY correct OPINION Feb 06 '11 at 15:02
13

HTTP is stateless - this means that when using HTTP the end point does not "remember" things (such as who you are). It has no state. This is in contrast to a desktop application - if you have a form and you go to a different form, then go back, the state has been retained (so long as you haven't shut down the application).

Normally, in order to maintain state in web application, one uses cookies.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    can u give example of protocol which remember state so that i can compare –  Feb 06 '11 at 14:34
  • 1
    @Name - [TCP](http://en.wikipedia.org/wiki/Transmission_Control_Protocol) is a protocol that retains some state. – Oded Feb 06 '11 at 14:36
  • 1
    although not intended por network use the XYZModem family of protocols (XModem, YModem, and so son) are stateful – alvaroc Jul 19 '15 at 00:56
  • ‘HTTP is stateless - this means that when using HTTP the end point does not "remember" things (such as who you are). It has no state.’ Not exactly. It means that the server does not store client-specific server state, known as *application state*, which therefore is stored by each client. But it can still store client-shared server state, known as *resource state*. – Géry Ogam May 15 '21 at 14:49
5

A stateless protocol does not require the server to retain information or status about each user for the duration of multiple requests. For example, when a web server is required to customize the content of a web page for a user, the web application may have to track the user's progress from page to page.

A common solution is the use of HTTP cookies. Other methods include server side sessions, hidden variables (when the current page is a form), and URL-rewriting using URI-encoded parameters, e.g., /index.php?session_id=some_unique_session_code.

here

Luis
  • 5,979
  • 2
  • 31
  • 51
4

HTTP is called a stateless protocol because each command is executed independently, without any knowledge of the commands that came before it.

This shortcoming of HTTP is being addressed in a number of new technologies, including cookies.

ykombinator
  • 2,724
  • 7
  • 25
  • 46
  • ActiveX, Java and Javascript do not address the fact that http is stateless, they leverage on cookies, url rewriting, etc to maintain state – Luis Feb 06 '11 at 14:19
3

When it's said that something is stateless it usually means that you can't assume that the server tracks any state between interactions.

By default the HTTP protocol assumes a truly stateless server. Every request is treated as an independent request.

In practice this is fixed by some servers (most of them) using a tracking cookie in the request to match some state on the server with a specific client. This works because the way cookies work (they are posted to server on each subsequent requests once they have been set on the client).

Basically a server that isn't stateless is an impediment to scale. You need to either make sure that you route all the requests from a specific browser to the same instance or to do backend replication of the states. This usually is a limiting factor when trying to scale an application.

There are some other solutions for keeping track of state (see rails's encrypted state cookie) but basically if you want to grow you need to figure a way to avoid tracking state on the server :).

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33