In This Chapter:
This chapter talks about several topics. Among them are session state, application state, caching, and using cookies. With all these topics, the intention is to preserve some information about the application and a particular session. Session state can include things such as user's selected colors, user information, or user selections (such as ordering information). Application state, or information, might include things such as a counter of how many visitors the site has had today, or maybe even something as simple as the date and time. Cached objects can be pretty much anything, but they are used to improve the performance of a Web application by keeping commonly used data in memory. To discuss using cookies requires a brief introduction to the concept of state.
HTTP is a stateless protocol. Let's look at what this means. A client makes a request by providing the HTTP server with some data such as IP address, browser type, and operating system. The server grinds on that data and creates some output. At that point, the client and server disconnect from each other. Any new client requests require that the client provide more information for the server to act. This is why we say that HTTP is stateless.
Using cookies is one way to maintain some state. But clients can prevent cookies from being saved by altering the browser settings and when this happens, using cookies to maintain state is impossible. ASP.NET provides an even better mechanism for maintaining a certain amount of state between a client and a server, and this mechanism has two distinct categories. In the first category, state can be maintained as application variables. Application state affects all users; session state affects individual users. ASP.NET lets you remember things based on individuals or based on everything. Application variables hold data from the time the application starts until the application ends. That is not to say that data doesn't change; it is to say that data is maintained in whatever state it happens to be for the life of the application. Another category of variables that can maintain state are called session variables. These variables contain information about the current session. For example, if a user prefers that all of his backgrounds be blue, a session variable can indicate this. And if a user is identified with some sort of ID, this information also can be kept in a session variable.
In this chapter, you are going to learn about several topics related to maintaining state. Of course, application and session variables will be important topics. We will 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.