当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Global.asa
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 文件是一种可选性文件,它包括了对象、变量以及方法的声明,而这些对象、变量以及方法可以被每个ASP程序文件访问到。
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. All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.
Global.asa 文件是一种可选性文件,它包括了对象、变量以及方法的声明,而这些对象、变量以及方法可以被每个ASP程序文件访问到。所有能被浏览器支持的脚本语言(比如:JavaScript,VBScript,Jscript,PerlScript或其他语言)都可以在“Global.asa”文件中使用。
The Global.asa file can contain only the following:
“Global.asa”文件尽可以包含以下内容:
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 file.
注意: “Global.asa”文件必须存放在ASP程序文件夹的根目录下,每个ASP程序只能使用一个“Global.asa”文件。
In Global.asa you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed in event handlers. The Global.asa file can contain four types of events:
在“Global.asa”文件中,你可以分别在Application或Session对象开始或结束的时候设定他们所要实现的功能。与其所对应的代码被放置在事件处理程序的内部。“Global.asa”文件可以包含四种类型的事件。
Session_OnStart - This event occurs EVERY time a NEW user requests his or her first page in the ASP application.
Session_OnStart:当一个新用户每次请求ASP页面时,这个事件都将被触发。
Session_OnEnd - This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specified time (by default this is 20 minutes).
Session_OnEnd:当一个用户结束Session时,这个事件将被触发。如果在一个特定的时间下(一般是20分钟),用户没有再继续请求发送页面,那么Session将结束。
Application_OnEnd - This event occurs after the LAST user has ended the session. Typically, this event occurs when a Web server stops. This procedure is used to clean up settings after the Application stops, like delete records or write information to text files.
Application_OnEnd:当最后一个发出结束请求的用户结束Session时,那么这个事件被触发。最典型的情况就是,当服务器停止后,这个事件将会被触发。这个程序是用来清除所有在Application结束之后的设置,比如删除文本文件中的记录或者往文本文件中写入新的信息。
A Global.asa file could look something like this:
一个“Global.asa”文件可以类似于以下形态:
<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: Because we cannot use the ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we put subroutines inside an HTML <script> element.
注意:因为我们不能在“Global.asa”文件中使用ASP脚本符号(<% %>),所以我们可以把<Script>放在HTML页面内使用。
It is possible to create objects with session or application scope in Global.asa by using the <object> tag.
通过在Global.asa文件中的使用<object>标签,可以建立Session和Application范围内的对象。
Note: The <object> tag should be outside the <script> tag!
提示:<object>标签必须写在<script>标签之外。
<object runat="server" scope="scope" id="id" {progid="progID"|classid="classID"}> .... </object> |
Parameter 参数 | Description 描述 |
---|---|
scope | Sets the scope of the object (either Session or Application) 设置Session或Application对象的范围 |
id | Specifies a unique id for the object 为每一个对象指定一个独立的id |
ProgID | An id associated with a class id. The format for ProgID is [Vendor.]Component[.Version] 含有ClassID的id. ProgID 的格式是: [Vendor.]Component[.Version] Either ProgID or ClassID must be specified. |
ClassID | Specifies a unique id for a COM class object. 为一个 COM class 对象制定一个id. Either ProgID or ClassID must be specified. |
The first example creates an object of session scope named "MyAd" by using the ProgID parameter:
第一个案例是通过使用ProgID参数在名为“MyAd”的Session范围内建立一个对象。
<object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"> </object> |
The second example creates an object of application scope named "MyConnection" by using the ClassID parameter:
第二个案例是通过使用ClassID参数在名为“MyConnection”的Application范围内建立一个对象。
<object runat="server" scope="application" id="MyConnection" classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21"> </object> |
The objects declared in the Global.asa file can be used by any script in the application:
在Global.asa文件当中声明的所有对象都可以被Application中的脚本程序使用。
GLOBAL.ASA: <object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"> </object> You could reference the object "MyAd" from any page in the ASP application: SOME .ASP FILE: <%=MyAd.GetAdvertisement("/banners/adrot.txt")%> |
A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.
TypeLibrary包括了所有与COM对象相对应的DLL文件的内容集合。通过调用Global.asa文件中的TypeLibrary,我们可以COM对象中的常量,而且产生的错误可以通过ASP的代码更好的反馈出来。如果你的网页请求是依赖于COM对象的,而此时,这个COM对象已经在类型库中声明了数据类型,那么你就可以在Global.asa文件中声明这个类型库。
<!--METADATA TYPE="TypeLib" file="filename" uuid="typelibraryuuid" version="versionnumber" lcid="localeid" --> |
Parameter 参数 | Description 具体描述 |
---|---|
file | Specifies an absolute path to a type library. Either the file parameter or the uuid parameter is required |
uuid | Specifies a unique identifier for the type library. Either the file parameter or the uuid parameter is required |
version | Optional. Used for selecting version. If the requested version is not found, then the most recent version is used 这个参数是可选的。这个参数是用来选择版本的,如果所需要的版本信息没有被找到,那么将自动选择最近一次使用的版本信息 |
lcid | Optional. The locale identifier to be used for the type library 这个参数是可选的。被类型库所使用的本地标志符 |
The server can return 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 appear anywhere in the Global.asa file (both inside and outside <script> tags). However, it is recommended that METADATA tags appear near the top of the Global.asa file.
提醒:METADATA标签可以在Global.asa文件中的任何地方使用(无论是在<script>标签里面或是外面)。然而,我们推荐将METADATE标签在Global.asa文件中的最上方使用。
Restrictions on what you can include in the Global.asa file:
Global.asa文件的限制包括:
Global.asa is often used to initialize variables.
Global.asa通常是用来初始化变量的
The example below shows how to detect the exact time a visitor first arrives on a Web site. The time is stored in a Session variable named "started", and the value of the "started" variable can be accessed from any ASP page in the application:
下面的例子讲告诉我们如何探测到某个用户第一次访问网站的精确时间。这个变量被储存在名为“Started”的Session变量中,“Started”变量的值可以被任何一个ASP页面中的应用程序所访问到。
<script language="vbscript" runat="server"> sub Session_OnStart Session("started")=now() end sub </script> |
Global.asa can also be used to control page access.
Global.asa也可以控制页面的访问
The example below shows how to redirect every new visitor to another page, in this case to a page called "newpage.asp":
下面的例子告诉我们如何链接到其他页面,在这个案例中,这个被链接的页面的文件名是“newpage.asp”:
<script language="vbscript" runat="server"> sub Session_OnStart Response.Redirect("newpage.asp") end sub </script> |
And you can include functions in the Global.asa file.
同时,你也可以将Function(函数)写入Global.asa文件。
In the example below the Application_OnStart subroutine occurs when the Web server starts. Then the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. The record set is assigned to an array, where it can be accessed from any ASP page without querying the database:
当服务器开始工作时,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> |
In this example we will create a Global.asa file that counts the number of current visitors.
在这个例子当中,我们将建立一个全新的Global.asa文件,它可以计算出当前访问者的数量
The Global.asa file:
Global.asa文件如下:
<script language="vbscript" runat="server"> Sub Application_OnStart Application("visitors")=0 End Sub Sub Session_OnStart Application.Lock Application("visitors")=Application("visitors")+1 Application.UnLock End Sub Sub Session_OnEnd Application.Lock Application("visitors")=Application("visitors")-1 Application.UnLock End Sub </script> |
To display the number of current visitors in an ASP file:
在ASP文件中显示当今访问者的数量:
<html> <head> </head> <body> <p> There are <%response.write(Application("visitors"))%> online now! </p> </body> </html> |