Internet

Active Server Pages Tutorial

Working with ASP Applications

The Global.asa File

The Global.asa file stores the subroutines that are processed when an ASP application starts and ends. It is often used to declare variables and create objects such as database connections. The file is not required to run an application, but including it prevents you from having to include the same code on each page in your application. Code in the Global.asa file is not displayed, so the contents of the file should be limited to tasks required by the application to run. Note, too, that you cannot call subroutines contained in the Global.asa file from elsewhere in the application. You can create the file using a text editor. It should be given the file name Global.asa and saved in the root directory of the application. When an application starts, the server will look for and run this file first.

Working with Subroutines

All the code to be processed in the Global.asa file must be contained in one of four predefined subroutines: Application_OnStart, Application_OnEnd, Session_OnStart and Session_OnEnd. The Application_OnStart and Application_OnEnd subroutines let you perform tasks that will be processed when the application starts (for example, declaring variables) and ends (for example, removing variables). Similarly, the Session_OnStart and Session_OnEnd subroutines let you perform tasks to be processed at the start and end of sessions. You can, however, create your own subroutines, as long as they are called from within one of the four predefined subroutines. Subroutines should be enclosed in the

tags. The RUNAT attribute specifies that the script should be run on the server.

// <![CDATA[
Sub Application_OnStart

Application(“AppName”)=”Holiday Tips “

Application(“Holiday”)=”Valentine’s Day”

End Sub

Sub Application_OnEnd

Application(“Holiday”)=””

End Sub


// ]]>

Creating Objects

You can declare an object in the Global.asa file that will be used by multiple pages in the application. To do so, use the tags:

// <![CDATA[
Sub Application_OnStart

Application(“AppName”)=”Holiday Tips “

Application(“Holiday”)=”Valentine’s Day”

End Sub

Sub Application_OnEnd

Application(“Holiday”)=””

End Sub


// ]]>

The RUNAT attribute again specifies that the object should be created on the server. The SCOPE attribute specifies whether the object is session- or application-level. The ID attribute lets you name the object; you can then use this name to access the object from within your ASP pages. The PROGID and CLASSID attributes specify the unique identifier for the type of object being created. You must include one of these. If you’re creating an ActiveX object, for example, you can use the CLASSID attribute, which specifies a string value that uniquely identifies the component. To access the object from a page in the application, simply precede the name with the equal sign (=):

<%=Ad1.GetAdvertisement(“/ecards.txt”)%>

Declaring Type Libraries

In IIS5 and later, you can use the Global.asa file to declare a type library to bind to the application. Type libraries are files containing information about a particular COM object. Declaring a type library in Global.asa lets you access the constants for the object from any page in the application. To declare a type library, you use the tag:

<!– METADATA NAME=”Microsoft ActiveX Data Objects 2.5 Library” TYPE=”TypeLib” UUID=”{00000205-0000-0010-8000-00AA006D2EA4}” –> // <![CDATA[
Sub Application_OnStart

Application(“AppName”)=”Holiday Tips “

Application(“Holiday”)=”Valentine’s Day”

End Sub

Sub Application_OnEnd

Application(“Holiday”)=””

End Sub


// ]]>

The UUID attribute specifies the unique identifier for the type library. You must use either the UUID attribute or the FILE attribute, which specifies the physical location of the library:

<!– METADATA NAME=” Microsoft ActiveX Data Objects 2.5 Library” TYPE=”TypeLib” FILE=”c:\program files\common files\system\ado\msado15.dll” –> <script runat=”Server” language=”VBScript” type=”text/javascript”> Sub Application_OnStart Application(“AppName”)=”Holiday Tips “ Application(“Holiday”)=”Valentine’s Day” End Sub Sub Application_OnEnd Application(“Holiday”)=”” End Sub </script> <object progid=”MSWC.AdRotator” id=”Ad1″ scope=”Session” runat=”Server”>

Other optional attributes are VERSION and LCID. Version specifies the major and minor version numbers (in the form VERSION=”Major.Minor”) and LCID specifies the locale identifier. If the local is not specified, the System locale is used. Once you declare a type library, you can create an instance of the object in any ASP page in the application, and then access the constants that have been declared for the object:

<% Set rs = Server.CreateObject(“ADODB.Recordset”) rs.ActiveConnection = cnnStuff rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic %>

Server Side Includes (SSIs)

Server Side Includes are references in ASP pages to files containing reusable chunks of code. These files can contain HTML or ASP, and are saved with the extension .inc. The following example demonstrates a file containing a database connection that will be reused in multiple pages in an application:

<% Public cnn, cnnstr, rst Public folder, path Public sql, cnt, i, msg Public Function InitDB() folder=Server.MapPath(“/Documents/docs.mdb”) set cnn=createobject(“adodb.connection”) set rst=createobject(“adodb.recordset”) cnnstr = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ” & Server.MapPath(“/Documents/docs.mdb”) With Cnn .Mode = adModeReadWrite .Mode = adModeShareDenyNone .CursorLocation = 3 .ConnectionString = cnnstr .open End With End Function %>

The physical path to the file is then included in the various ASP pages using the #include file statement enclosed in comment tags:

<!–#include file=”Connections\data.inc” –>

You can use the VIRTUAL keyword to include a file contained in a virtual directory outside the directory structure of the application:

<!–#include virtual=”IncludeFiles/data.inc” –>