Techniques for Retaining State Information in ASP Applications
Developers who use Visual Basic (VB) to build Windows applications are familiar with the concept of module-scope variables. If a variable is declared module-scope within a form, the variable exists and retains its value as long as the form is loaded into memory. Module-scope variables make it a trivial matter for a Windows application to retain data between interactions with the user. However, retaining this data, typically called state information or sometimes context information, is not a trivial matter for Web application developers.
HTTP and State Information
Retaining state information in Web applications is difficult because Web browsers use the HTTP protocol for communicating with Web servers; and HTTP is a stateless, request-response protocol. This means that the following processes happen each time a user clicks a hyperlink on a Web page:
The browser client establishes a connection to the Web server.
The browser sends the server a request.
The server processes the request.
The server sends a response back to the client.
The connection between the client and server is promptly closed.
The server forgets everything about the request it just processed, so it has no way of associating a request from a client with any previous requests from the same client. The server doesn't know when a user first enters a site or when the user leaves the site. When all a Web server had to do was to send static HTML pages, HTTP worked fine. But imagine trying to write a Windows application using nothing but event procedures and local-scope variables, and you get an idea of why this is problematic for Web application developers.
Any real Web application will need to retain state information, so Web application developers use several coding techniques to retain state. In this article, I'll discuss the five state retention techniques that are commonly used, along with the relative advantages and disadvantages of each.