Detailed introduction to Global.asa files in ASP

Source: Internet
Author: User
The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by each page in an ASP application.

Global.asa file

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by each page in an ASP application. All legitimate browser scripts can be used in Global.asa.

The Global.asa file can contain the following content:

Application Events

Session Event

<object> statement

TypeLibrary statement

#include instructions

Note: The Global.asa file must be stored in the root directory of the ASP application and can have only one Global.asa file per application.

Events in the Global.asa

In Global.asa, we can tell application and session objects what to do when they start and end. The code that completes the task is placed in the event operator. The Global.asa file can contain four types of events:

Application_OnStart -This event occurs when the first page is called from an ASP application by the first user. This event occurs after the Web server restarts or the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event occurs.

Session_OnStart -This event occurs whenever a new user requests his or her first page in an ASP application.

Session_OnEnd -This event occurs every time the user ends the Session. At the specified time (the default event is 20 minutes), if no page is requested, the session ends.

Application_OnEnd -This event occurs after the last user ends his session. Typically, this event occurs when the WEB server is stopped. This subroutine is used to purge settings after the application has stopped, such as deleting records or writing information to a text file.

The Global.asa file may resemble this:

<script language= "VBScript" runat= "Server" >sub Application_OnStart  ' some codeend SubSub Application_OnEnd  ' Some codeend subsub Session_OnStart  ' some codeend subsub Session_OnEnd  ' some codeend sub</script>

Note: Because scripts cannot be inserted in the Global.asa file using the ASP's script delimiter (<% and%>), we need to use the HTML <script> element.

<object> statement

You can create an object with a session or application scope in the Global.asa file by using the <object> tag.

Note The:<object> label should be located outside the <script> label.

Grammar:

<object runat= "Server" scope= "Scope" id= "id" {progid= "ProgID" |classid= "ClassID"}>....</object>

Parameter description

Scope sets the scope (scope) of the object (Session or application).

ID Specifies a unique ID for the object.

The ID of the ProgID associated with the ClassID. The format of the ProgID is: [Vendor.] component[. Version]. The ProgID or ClassID must be specified.

ClassID specifies a unique ID for the COM class object. The ProgID or ClassID must be specified.

Instance

The first instance creates a session scope object named "Myad" that uses the ProgID parameter:

<object runat= "Server" scope= "session" id= "Myad" progid= "MSWC. AdRotator "></object>

The second instance creates a myconnection with the ClassID parameter named "

<object runat= "Server" scope= "Application" id= "MyConnection" classid= "Clsid : 8ad3067a-b3fc-11cf-a560-00a0c9081c21 "></object>

These objects declared in this Global.asa file can be used by any script in the application.

GLOBAL. Asa:

<object runat= "Server" scope= "session" id= "Myad" progid= "MSWC. AdRotator "></object>

You can reference this "Myad" object from any page in an ASP application:

A. ASP files:

<%=myad.getadvertisement ("/banners/adrot.txt")%>

TypeLibrary statement

A typelibrary (type library) is a container that contains a DLL file that corresponds to a COM object. By including calls to TypeLibrary in Global.asa, you can access the constants of COM objects, and ASP code can better report errors. If your site's application relies on COM objects that have already declared data types in the type library, you can declare the type library in Global.asa.

Grammar:

<!--METADATA type= "TypeLib" file= "filename" uuid= "Typelibraryuuid" version= "versionnumber" lcid= "LocaleID"--

Parameter description

The file specifies the absolute path to the type library. parameter file or UUID, both are indispensable.

The UUID specifies a unique identifier for the type library. parameter file or UUID, both are indispensable.

Version is optional. Used to select a version. If the specified version is not found, the closest version will be used.

The LCID is optional. The locale identifier used for the type library.

Error value

The server returns one of the following error messages:

Error code description

ASP 0222 Invalid Type library specification

ASP 0223 Type Library not found

ASP 0224 Type Library cannot be loaded

ASP 0225 Type Library cannot be wrapped

Note : The METADATA tag can be located anywhere in the Global.asa file (both inside and outside of the <script> tag). However, we recommend placing the METADATA tag at the top of the Global.asa file.

Qualified

About the qualification of the content that can be referenced in the Global.asa file:

You cannot display the text in the Global.asa file. This file cannot display information.

You can only use the Server and application objects in the Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use the Server, application, and Session objects. In the Session_OnStart subroutine, you can use any of the built-in objects.

How to use subroutines

Global.asa are often used to initialize variables.

The following example shows how to detect the exact time when a visitor first arrives at the site. The time is stored in a Session object named "Started", and the value of the "started" variable can be accessed by any ASP page in the application:

<script language= "VBScript" runat= "Server" >sub session_onstartsession ("Started") =now () End sub</script>

Global.asa can also be used to control page access.

The following example shows how to redirect each new visitor to another page, which in this case is directed to the "newpage.asp" page:

<script language= "VBScript" runat= "Server" >sub session_onstartresponse.redirect ("newpage.asp") End sub</ Script>

We can also include functions in Global.asa.

In the following example, when the Web server starts, the Application_OnStart subroutine is also started. The Application_OnStart subroutine then invokes another subroutine named "GetCustomers". The "GetCustomers" subroutine opens a database and then retrieves a recordset from the "Customers" table. This recordset is assigned to an array, and any ASP page can access the array without querying the database:

<script language= "VBScript" runat= "Server" >sub application_onstartgetcustomersend SubSub GetCustomers set conn= Server.CreateObject ("ADODB. Connection ") Conn. Provider= "Microsoft.Jet.OLEDB.4.0" Conn. The Open "C:/webdata/northwind.mdb" Set Rs=conn.execute ("Select name from Customers") application ("Customers") =rs. GetRowsrs.Closeconn.Closeend sub</script>

Global.asa instances

In this example, we are going to create a Global.asa file that calculates the current visitor.

Application_OnStart set when the server starts, the value of the application variable "visitors" is 0.

Each time a new user accesses, the Session_OnStart subroutine adds 1 to the variable "visitors".

Each time the Session_OnEnd subroutine is triggered, this subroutine is reduced by 1 from the variable "visitors".

Global.asa file:

<script language= "VBScript" runat= "Server" >sub application_onstartapplication ("visitors") =0end SubSub Session _onstartapplication.lockapplication ("visitors") =application ("visitors") +1application.unlockend SubSub Session_ Onendapplication.lockapplication ("visitors") =application ("visitors") -1application.unlockend sub</script> This The ASP file is displayed when

Number of former users:


"Recommended"

ASP free Video Tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.