The Application Object

Using Events in the Global.asa File

Global.asa is the first file a Web server processes when it receives the first user request for an .asp file after start-up, or when a user without a session requests an .asp file.

A Web application can have one Global.asa file. This file is stored in the virtual root of the application.

You can include the following information in a Global.asa file:

  • Application start events, end events, or both.
  • Session start events, end events, or both.
  • <OBJECT> tags that create components with application or session scope.
If you use the Web Project Wizard to create your Web application, Microsoft Visual InterDev creates a Global.asa file with event procedure templates and comments to help you use this file.

Application Object Events

The Application object has two events, Application_OnStart and Application_OnEnd . You can add script to these events in the Global.asa file.

Any script you add to the Application_OnStart event runs when the application starts. Conversely, any script you add to the Application_OnEnd event runs when the application ends.

Example

This example adds the Application_OnStart event to the Global.asa file:
 <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Application_OnStart 'Your Code Here End Sub </SCRIPT> 

Session Object Events

Similar to the Application object, the Session object has the events Session_OnStart and Session_OnEnd . Any script you add to the Session_OnStart event runs when a user without an existing session requests an .asp file from your application. Any script you add to the Session_OnEnd event runs when a user session ends.

Example

This example uses the Session_OnStart event to direct users to a page that logs on to your site, regardless of which ASP they request from your Web application:
 ' When a session starts, redirect the user to a logon page Sub Session_OnStart If Session("username") = "" Then ' Save the name of the page the user wanted to visit Session("startpage") = _ Request.ServerVariables("SCRIPT_NAME") ' Redirect them to a logon page Response.Redirect "profile.htm" End If End Sub 

Using the <OBJECT> Tag in the Global.asa File

You can use the <OBJECT> tag in the Global.asa file to create a component that runs on the Web server.

To use the <OBJECT> tag in a Global.asa file, set the RUNAT attribute to Server and the SCOPE attribute to Application or Session . To specify the component, you can use its registered name, PROGID, or its registered number, CLASSID.

Example

This example uses the PROGID to create a session-scope instance of the Advertisement Rotator component:
 <OBJECT RUNAT=Server SCOPE=Session ID=MyAd PROGID="MSWC.Adrotator"> </OBJECT> 
Example This example uses the registered number (CLASSID) method to create an application-scope instance of the Advertisement Rotator component:
 <OBJECT RUNAT=Server SCOPE=Application ID=MyAd  CLASSID="Clsid:00000293-0000-0010-8000-00AA006D2EA4"></OBJECT> 
When you use the <OBJECT> tag to declare a session-scope or application-scope instance of a component, the variable you assign to the component is stored in the session or application namespace. You do not need to use the Session or Application objects to access the instance of the component.

Example

This example opens the instance of the Advertisement Rotator component that has been declared in the previous example code:
 <%= MyAd.GetAdvertisement("addata.txt") %> 

Lesson Summary

Active Server Pages allow you to maintain state in a Web application by using cookies, the Session object, or the Application object. Cookies save state information on a user's computer. The user's Web browser sends the cookie back to the server during each subsequent visit to that page or number of pages.

The Session object differs from a cookie in that it saves state information on the server. The Session object stores information that is needed for a particular user. The Web server automatically creates a Session object when a session starts. When the session expires or is abandoned , the Web server destroys the Session object. The Session object has two associated events: Session_OnStart and Session_OnEnd .

You can create and store values in the Application object just as you can the Session object. The difference is that the information stored in the Application object is available from the time the first user makes and HTTP request for an ASP until the Web server shuts down. The Application object has two associated events: Application_OnStart , and Application_OnEnd . You can add script to these in the Global.asa file.



Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net