In this chapter
HTTP, by definition, is stateless. Browsers connect to Web sites, request pages and other data, and then act on the data that they have received from the server. After this data transfer is done, though, there is no more interaction between the client and the server, including shared data and state variables. This is why we say that HTTP is stateless.
Using cookies is one way to maintain some state. But ASP.NET provides an even better mechanism for maintaining a certain amount of state between a client and server, and it has two distinct categories. Client browsers can prevent cookies from being saved by altering the browser user settings ”and when this happens, using cookies to maintain state isn't possible. In the first category, state can be maintained with application variables. Application variables hold data from the time the application starts until the application ends. That is not to say that the data doesn't change; it is to say that the data is maintained in whatever state it happens to be for the life of the application.
Another category of variables that can maintain state is called session variables. These variables contain information about the current session. For example, if a user prefers that all his backgrounds be blue, a session variable could indicate this. And a user might be identified with some sort of unique ID ”this also could be kept in a session variable.
In this chapter, we're going to learn about several topics related to maintaining state. Of course, application and session variables will be important topics. We'll have to talk about synchronizing access to these variables because there will be times when only one client session can access or change the value of a state variable at any given time. The Global.asax file plays an important role in maintaining the values of application and session variables because it contains methods that are called when a session starts and ends, and when an application starts and ends.