Developing Web Applications

Previous Topic Next Topic

Application and Session Events

Every application has two events associated with it: Application_OnStart and Application_OnEnd. The script for these events is defined with server-side <SCRIPT> tags within Global.asa. Event script can be written in any language supported by the server. Application_OnStart is called once for each application, when the first client makes a request for a page within the application boundaries. The Application_OnStart event procedure is a good place to set global state variables and to create any objects that will be used by all users of the application.

After the Application_OnStart event, and for each subsequent new session, the Session_OnStart event occurs. An ASP application should use the Session_OnStart event to perform any required session initialization tasks. At this point, a Session object and a Request object exist. The Session object includes a unique session ID; the Request object includes fully parsed collections of values passed by the browser, as well as the server environment variables.

The Session_OnStart event procedure is a good place to redirect to the start page of your application. If you don’t redirect, the application begins execution with the document requested in the URL, or it will begin with one of the default documents configured in Internet Services Manager for the application’s virtual root. Redirection allows you to control where your application will go first.

Since the Response.Write method is not available during the processing of event procedures, you won’t immediately be able to report errors if they occur. However, if you want to notify the user of any problems, you can save the error text in the Session or Application object (depending on which event handler caused the error) and report it on the first page that is loaded thereafter.

A session ends either when it times out or when the Session.Abandon method is called. When this happens, the Session_OnEnd event procedure is called, giving you the chance to destroy any object references, and perform any other session cleanup. Of the server built-in objects, only the Application, Session and Server objects are available to the OnEnd event handlers. Additionally, you can’t use the MapPath or CreateObject methods of the Server object during the Session_OnEnd event.


© 1997-1999 Microsoft Corporation. All rights reserved.