当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Global.asa

ASP
ASP Drive
ASP File
ASP Folder
ASP Dictionary
ASP ADO
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator
ASP Quick Ref
ASP 摘要
ASP 实例

ASP Global.asa


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 786 ::
收藏到网摘: n/a

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
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. 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”文件尽可以包含以下内容:

  • Application events
    程序事件
  • Session events
    Session事件
  • <object> declarations 
    <object>声明
  • TypeLibrary declarations
    TypeLibrary声明
  • the #include directive
    #include指令

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”文件。


Events in Global.asa
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”文件可以包含四种类型的事件。

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 after the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.
Application_OnStart:当第一个发出请求的用户请求ASP文件中的程序时,这个事件将被触发;当服务器重启或者“Global.asa”文件被重新编辑的时候,这个事件将被触发;在上述事件之后,“Session_OnStart”将被触发。

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页面内使用。


<object> Declarations
<object> 声明

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>标签之外。

Syntax
语法

<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.
ProgID 和 ClassID 都必须被定义

ClassID Specifies a unique id for a COM class object.
为一个 COM class 对象制定一个id. 

Either ProgID or ClassID must be specified.
ProgID 和 ClassID 都必须被定义

Examples
举例

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")%> 


TypeLibrary Declarations
声明一个TypeLibrary(类型库)

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文件中声明这个类型库。

Syntax
语法

<!--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
给类型库指定一个绝对路径,包括file(文件)参数以及uuid参数。

uuid Specifies a unique identifier for the type library.

Either the file parameter or the uuid parameter is required
给类型库设定一个独立的标志符,包括file(文件)参数以及uuid参数。

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
这个参数是可选的。被类型库所使用的本地标志符

Error Values
错误值

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
限制

Restrictions on what you can include in the Global.asa file:
Global.asa文件的限制包括:

  • You can not display text that is written in the Global.asa file. This file can't display information
    你不可以浏览写在Global.asa文件中的文本。因为Global.asa文件没有显示信息的功能。
  • You can only use Server and Application objects in the 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
    你只能使用Server和Application对象中Application_OnStart和Application_OnEnd的子程序。在Session_OnEnd子程序中,你可以使用服务器对象、Application对象和Session对象;在Session_OnStart子程序中,你只能使用内置的对象。

How to use the Subroutines如何使用子程序

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>


Global.asa Example
Global.asa举例

In this example we will create a Global.asa file that counts the number of current visitors.
在这个例子当中,我们将建立一个全新的Global.asa文件,它可以计算出当前访问者的数量

  • The Application_OnStart sets the Application variable "visitors" to 0 when the server starts 
    • 当服务器开始工作时,Application_OnStart将名为"visitors"的Application变量的值着值为0。
  • The Session_OnStart subroutine adds one to the variable "visitors" every time a new visitor arrives
    • 当每次有新的访问者访问页面时,Session_OnStart子程序就将变量"visitors"自动加1。
  • The Session_OnEnd subroutine subtracts one from "visitors" each time this subroutine is triggered
    • 每次当Session_OnEnd子程序被触发时,他都会自动 将"visitors"变量的值减1。

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>

评论 (0) All

登陆 | 还没注册?