Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The books and documentation on the MVC just heap on using the Stateful and Stateless terms. To be honest, i am just unable to grab the idea of it, what the books are talking about. They don't give an example to understand any of the either state, rather than just telling that HTTP is stateless and with ASP.NET MVC microsoft is going along with it. Am I missing some fundamental knowledge, as i can't understand what is stateful and why is stateful and same goes for stateless.

A simple and short example that talks about a control like button or textbox can be simplify the understanding i suppose.

share|improve this question

migrated from stackoverflow.com Aug 16 '11 at 0:39

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

up vote 14 down vote accepted

Stateless means that HTTP doesn't have built in support for states. i.e. you can't store if a user have logged in or done something else.

The most common solution is to use sessions to overcome that problem. This means that you have to be able to include a session identifier in each response or request. It's either done by creating a session cookie or by including the session identifier in all links.

WebForms tried to make all that transparent (using ViewState) while MVC forces you to handle it manually.

In your example you mentioned buttons and textboxes. The easiest way to let them maintaining their state is simply to stop posting back the entire page. MVC got excellent support for ajax (through jQuery) and I suggest that you use ajax if you just want to do something on the current page.

share|improve this answer
1  
this means if i am logged in a website, every page i visit just rechecks for authentication before rendering the content via session identifier or a session cookie ?? –  Pankaj Upadhyay Aug 15 '11 at 6:43
2  
yes, that's correct. –  jgauffin Aug 15 '11 at 7:03
    
so is stateless better or worse? –  think123 Apr 2 at 7:19
    
@think123: It's more performant as you do not have to manage state (i.e. load balancing etc is a lot easier). It's how ever more complex since you have to manage an artificial state. –  jgauffin Apr 3 at 6:53
add comment

To illustrate the concept of state I'll define a function which is stateful and one which is stateless

Stateless

//The state is derived by what is passed into the function

function int addOne(int number)
{
    return number + 1;
}

Stateful

//The state is maintained by the function

private int _number = 0; //initially zero 

function int addOne()
{
    _number++;
    return _number;
}

As others have said http is inherently stateless. So state must be built into your applications.

Imagine a request over the web where you have a client browser communicating to a server process. To maintain state over the stateless http protocol the browser will send typically send a session identifier to the server on each request. For each request the server will be like "ah, its this guy". State information can then be looked up in server side memory or in a database based on this session id.

In a purely stateless environment you wouldn't need this session id. Each request would contain all the information the server would need to process. But many applications need to maintain state to keep track of whether or not a session is authenticated to view certain content or to keep track of what a user is doing. You wouldn't want to send user credentials over the wire for each request.

share|improve this answer
add comment

stateless means there is no memory of the past. Every transaction is performed as if it were being done for the very first time.

statefull means that there is memory of the past. Previous transactions are remembered and may affect the current transaction.

share|improve this answer
add comment

In my simple opinion the difference between ASP.NET (stateful) and ASP.NET-MVC (stateless) can be isolated to the fact that the first provides server-side controls and the other do not.

It is worth noting that the ASP.NET web forms approach is geared towards transitioning old VB and VC++ programmers who are used in the event model driven model to have a good quick way of learning web programming following the same event model paradigm, like you click a button and voila you trigger an event! All you need to do now is write your code in the event handler. Because of that ASP.NET needed to have concepts like view state and postbacks to monitor the state of the server side controls each round trip.

ASP.NET-MVC, however, does not use server-side controls so it doesn't have to maintain state. The MVC model separates the problem domain into the three partitions so that data is delivered to the client in a streamlined manner.

In summary, the server-side controls are what makes them different in as far as being stateful and stateless.

share|improve this answer
add comment

A stateful operation modifies or requires some state of the system, and a stateless operation does not.

Example of a stateful textbox would be a previously edited comment on StackExchange - the textbox needs to display your previous comment and know the post-thread it was involved with to accept and process your input.

A generic email comment form with a mailto: tag would be a stateless textbox - it accepts your input and tosses it to your workstation's mail application without retaining any information.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.