ASP Tutorial: Learn to Master ASP Global.asa files

Source: Internet
Author: User
Tags variables unique id uuid access root directory
Tutorial

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 contents:

    • Application Events
    • Session Events
    • <object> statement
    • TypeLibrary statement
    • #include命令

Note: Global.asa files must be stored in the root directory of the ASP application, and each application can have only one global.asa file.

Events in the Global.asa

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

This event occurs when the first page is called from an ASP application by the first user. This event occurs after the Web server is reset 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 whenever the user ends the session. The session ends at the specified time (the default event is 20 minutes) if no page is requested.

This event occurs after the last one has the end of its session. Typically, this event occurs when the Web server is stopped. This subroutine clears the settings after the application stops, such as deleting a record or writing information to a text file.

The Global.asa file might look like 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

Note: We need to use HTML <script> elements because we cannot insert scripts into global.asa files using the ASP's scripting delimiters (<% and%>).

<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>
Parameters Description
Scope Set the scope (scope) (session or application) of the object.
Id Specifies a unique ID for the object.
ProgID The ID associated with the classid. The ProgID format is: [Vendor.] component[. Version]. ProgID or ClassID must be specified.
ClassID Specify a unique ID for the COM class object. ProgID or ClassID must be specified.

Instance

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

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

The second instance creates a classid named "MyConnection" that uses the parameters of the

<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 are could reference the object "Myad" from the All page in the ASP application:

A. ASP file:

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

TypeLibrary statement

A typelibrary (type library) is a container that contains a directory of DLL files that correspond to COM objects. By referencing the call to TypeLibrary in Global.asa, you can access the constants of COM objects, and the ASP code can also 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"-->
Parameters Description
File Specify the absolute path to the type library. parameter file or UUID, both of which are integral.
Uuid A unique identifier for the type library is specified. parameter file or UUID, both of which are integral.
Version Optional. Used to select a version. If the specified version is not found, the closest version will be used.
Lcid Optional. Optional. The region 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: Metadata tags can be located anywhere in the Global.asa file (both inside and outside the <script> label). However, we recommend placing the metadata tag at the top of the Global.asa file.

Qualified

Qualification for content that can be referenced in a 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 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 a subroutine

Global.asa are often used to initialize variables.

The following example shows how to detect the exact time at which a visitor first arrives at the site. The time is stored in the 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 refer to functions in Global.asa.

In the following example, the Application_OnStart subroutine starts when the Web server is 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 that can be accessed by any ASP page 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 instance

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

Application_OnStart setting the value of the application variable "visitors" is 0 when the server is started.

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

Whenever 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 Onendapplication.lockapplication ("visitors") =application ("visitors") -1application.unlockend Sub</script>

This ASP file displays the number of current users:

 


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.