The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.
Global.asa file
The Global.asa file is an optional file that contains information that can be accessed by every page in an ASP application. Declaration of objects, variables, and methods. All legal browser scripts can be used in Global.asa.
The Global.asa file can contain the following:
Application event
Session event
TypeLibrary declaration
#include directive
Note: The Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa document.
Events in Global.asa
In Global.asa, we can tell the application and session objects what to do when starting and ending. The code to accomplish this 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 user calls the first page from an 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.
Session_OnStart - This event occurs every time a new user requests his or her first page in an 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 will end.
Application_OnEnd - This event occurs after the last user ends their session. Typically, this event occurs when the web server is stopped. This subroutine is used to clear settings after the application is stopped, such as deleting records or writing information to a text file.
Global.asa file may look like this:
<script language="vbscript" runat="server"> sub Application_OnStart 'some code end sub sub Application_OnEnd 'some code end sub sub Session_OnStart 'some code end sub sub Session_OnEnd 'some code end sub </script>
Note: Since ASP's script delimiters () cannot be used in Global.asa To insert a script into a file, we need to use the HTML <script> element. </script>
Objects with session or application scope can be created in the Global.asa file by using the
Note: The
Syntax:
<object runat="server" scope="scope" id="id" {progid="progID"|classid="classID"}> .... </object>
Parameters .
id Specify a unique id for the object.
ProgID The id associated with 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.
ExamplesThe 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 An instance is created named "MyConnection" using the ClassID parameter
<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 your ASP application:
A .ASP file:
<%=MyAd.GetAdvertisement("/banners/adrot.txt")%>
TypeLibrary Declaration
TypeLibrary (type library) is a container that holds DLL files corresponding to COM objects. By including a call to TypeLibrary in Global.asa, you can access the COM object's constants and your ASP code can better report errors. If your site's applications depend on COM objects whose data types have been declared in a type library, you can declare the type library in Global.asa.
Syntax:
<!--METADATA TYPE="TypeLib" file="filename" uuid="typelibraryuuid" version="versionnumber" lcid="localeid" -->
Parameters Syntax: Parameter file or uuid, both are indispensable.
uuid Specifies the unique identifier for the type library. Parameter file or uuid, both are indispensable.
version Optional. Used to select version. If the specified version is not found, the closest version will be used.
lcid Optional. The locale identifier used for the type library.
Error value
The server will return one of the following error messages:
错误 代码 描述
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
注释:METADATA 标签可位于 Global.asa 文件中的任何位置(在 <script> 标签的内外均可)。不过,我们还是推荐将 METADATA 标签放置于 Global.asa 文件的顶部。</script>
限定
关于可以在 Global.asa 文件中引用的内容的限定:
你无法显示 Global.asa 文件中的文本。此文件无法显示信息。
你只能在 Application_OnStart 和 Application_OnEnd 子例程中使用 Server 和 Application 对象。在 Session_OnEnd 子例程中,你可以使用 Server、Application 和 Session 对象。在 Session_OnStart 子例程中,你可使用任何内建的对象。
如何使用子例程
Global.asa 常用于对变量进行初始化。
下面的例子展示如何检测访问者首次到达站点的确切时间。时间存储在名为 "started" 的 Session 对象中,并且 "started" 变量的值可被应用程序中的任何 ASP 页面访问:
<script language="vbscript" runat="server"> sub Session_OnStart Session("started")=now() end sub </script>
Global.asa 也可用于控制页面访问。
下面的例子展示如何把每位新的访问者重定向到另一个页面,在这个例子中会定向到 "newpage.asp" 这个页面:
<script language="vbscript" runat="server"> sub Session_OnStart Response.Redirect("newpage.asp") end sub </script>
我们还可以在 Global.asa 中包含函数。
在下面的例子中,当 Web 服务器启动时,Application_OnStart 子例程也会启动。随后,Application_OnStart 子例程会调用另一个名为 "getcustomers" 的子例程。"getcustomers" 子例程会打开一个数据库,然后从 "customers" 表中取回一个记录集。此记录集会赋值给一个数组,在不查询数据库的情况下,任何 ASP 页面都能够访问这个数组:
<script language="vbscript" runat="server"> sub Application_OnStart getcustomers end sub sub 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.GetRows rs.Close conn.Close end sub </script>
Global.asa 实例
在这个例子中,我们要创建一个可计算当前访客的 Global.asa 文件。
Application_OnStart 设置当服务器启动时,Application 变量 "visitors" 的值为 0。
每当有新用户访问时,Session_OnStart 子例程就会给变量 "visitors" 加 1。
每当 Session_OnEnd 子例程被触发时,此子例程就会从变量 "visitors" 减 1。
Global.asa 文件:
<script language="vbscript" runat="server"> Sub Application_OnStart Application("visitors")=0 End Sub Sub Session_OnStartApplication.LockApplication("visitors")=Application("visitors")+1Application.UnLockEnd SubSub Session_OnEndApplication.LockApplication("visitors")=Application("visitors")-1Application.UnLockEnd Sub</script> 此 ASP 文件会显示当
前用户的数目:
<html> <head> </head> <body> <p>There are <%response.write(Application("visitors"))%> online now!</p> </body> </html>
【相关推荐】
The above is the detailed content of Detailed introduction to the Global.asa file in ASP. For more information, please follow other related articles on the PHP Chinese website!

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor
