Web Application

Textbook Chapter 9: Maintaining State

The HTTP protocol is a stateless protocol, meaning that each request for a page is processed without any knowledge of previous pages requested. The server has no record of previous browser requests. In an e-commerce application, we need to know who's credit card was just submitted for which order.

In this chapter, we will learn how to create application- and session-level variables that allow us to maintain information between individual users sessions. And, we'll learn to use cookies to maintain information across user sessions. Finally, we'll look at ways to maintain state without using cookies at all.

What is a Web Application?

A Web application is a group of files and folders located under the Web application's root directory. We can create scripts that run when the Web application starts and stops. These scripts are stored within a Global Application File. We can create instances of server objects, such as database access components, within the Global Application File, and then these objects are available to the entire Web application. We can also create application-level and session-level variables that are available to any page within the Web application.

Other advantages of defining a group of Web pages as a Web application is that you can run your Web application in its own memory space to prevent an error in one Web application from bringing down the rest of the Web applications on the server. This memory space is referred to as an isolated process, and is separate from the process that contains the IIS Web server. See http://msdn.microsoft.com/en-us/library/ms524990(v=vs.90).aspx.

A Web application can have only one Global Application File. The Global Application File is a text file called global.asa, which must reside in the root directory of the Web application. The Global Application File does not contain any HTML or client-side scripts. The Global Application File defines four subroutines, which run when the application starts and ends, and when the session starts and ends.

  1. Application_OnStart
  2. Application_OnEnd
  3. Session_OnStart
  4. Session_OnEnd

It is a good idea to keep all four subroutines listed in the Global Application File, even ones that are not used, since you might want to add code to the subroutines later.

The application and session objects are part of the ASP built-in object model. The application object allows you to maintain application state. You can maintain information across the entire Web application with the application object.

The client must support per-session cookies in order for ASP to maintain state within an application. A per-session cookie is used to allow the server to identify the client. The per-session cookie is temporary, and is deleted when the session ends.