Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Global.asa File in ASP

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of Global.asa files in ASP. I hope you will get something after reading this article. Let's discuss it together.

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 the 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 the ASP application. All legitimate browser scripts can be used in Global.asa.

The Global.asa file can contain the following:

Application event

Session event

Statement

TypeLibrary statement

# include directive

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

Events in Global.asa

In Global.asa, we can tell application and session objects what to do at startup and end. The code that accomplishes this task is placed in the event operator. Global.asa files can contain four types of events:

Application_OnStart-this event occurs when the first user invokes the first page from the ASP application. This event occurs after the web server is restarted 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 the ASP application.

Session_OnEnd-this event occurs whenever the user ends the session. If no page is requested within the specified time (the default event is 20 minutes), the session ends.

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

The Global.asa file might look something like this:

Sub Application_OnStart 'some codeend subsub Application_OnEnd' some codeend subsub Session_OnStart 'some codeend subsub Session_OnEnd' some codeend sub

Note: since you cannot insert a script in a Global.asa file using ASP's script delimiter (), we need to use the element of HTML.

Statement

You can create objects with session or application scopes in the Global.asa file by using tags.

Note: the label should be outside the label.

Syntax:

....

Parameter description

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

Id specifies a unique id for the object.

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

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

Example

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

The second instance creates an object named "MyConnection" that uses the ClassID parameter

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

GLOBAL.ASA:

You can reference this "MyAd" object from any page in the ASP application:

An .ASP file:

TypeLibrary statement

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

Syntax:

Parameter description

File specifies the absolute path to the type library. Parameter file or uuid, either of which is indispensable.

Uuid specifies a unique identifier for the type library. Parameter file or uuid, either of which is indispensable.

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

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 (inside and outside the tag). However, we recommend that you place the METADATA tag at the top of the Global.asa file.

Limit

Restrictions on what 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 Server and Application objects in Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use Server, Application, and Session objects. In the Session_OnStart subroutine, you can use any built-in object.

How to use subroutines

Global.asa is often used to initialize variables.

The following example shows how to detect exactly when visitors arrive at the site for the first time. 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:

Sub Session_OnStartSession ("started") = now () end sub

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

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

Sub Session_OnStartResponse.Redirect ("newpage.asp") end sub

We can also include functions in Global.asa.

In the following example, when the Web server starts, the Application_OnStart subroutine also starts. The Application_OnStart subroutine then calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and 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:

Sub Application_OnStartgetcustomersend subsub getcustomers set conn=Server.CreateObject ("ADODB.Connection") conn.Provider= "Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=conn.execute ("select name from customers") Application ("customers") = rs.GetRowsrs.Closeconn.Closeend sub

Global.asa instance

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

Application_OnStart sets the value of the Application variable "visitors" to 0 when the server starts.

The Session_OnStart subroutine adds 1 to the variable "visitors" whenever a new user accesses it.

Whenever the Session_OnEnd subroutine is triggered, it subtracts 1 from the variable "visitors".

Global.asa file:

Sub Application_OnStartApplication ("visitors") = 0End SubSub Session_OnStartApplication.LockApplication ("visitors") = Application ("visitors") + 1Application.UnLockEnd SubSub Session_OnEndApplication.LockApplication ("visitors") = Application ("visitors")-1Application.UnLockEnd Sub this ASP file displays when

Number of former users:

There are online now!

After reading this article, I believe you have some understanding of "sample Analysis of Global.asa documents in ASP". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report