ASP Getting Started Tutorial: ASP applocation global variable and Global.asa file

Source: Internet
Author: User

ASP Getting Started Tutorial: ASP global variable Global.asa file

The Global.asa file is an optional file that can contain declared objects, variables, and methods that can be used on each page of an ASP application.


-------------------------------------------------- ------------------------------

Global.asa file
The Global.asa file is an optional file that can contain declared objects, variables, and methods that can be used on each page of an ASP application. All valid browser scripts (JavaScript, scripting language, PerlScript, etc.) can be used in Global.asa.

The Global.asa file can only contain the following:

Apply Event
Conference activities
<object> statement
TypeLibrary statement
Include instruction in #
Note: The Global.asa file must reside in the ASP application in the root directory, and each application can have only one global.asa file.


-------------------------------------------------- ------------------------------

Activities in the Global.asa
Global.asa you can tell the application and the meeting object what to do when the application/meeting starts and what to do when applying/meeting the purpose. The code, which is placed in an event handler. The Global.asa file can contain four kinds of activities:

Application_OnStart-This event occurs when the first user requests the first page from the A SP application. After this event occurs, the Web server restarts or Global.asa file edits. The incident occurred immediately after the "Session_OnStart" incident.
Session_OnStart-This event occurs whenever a new user asks him or her on the first page of a SP application.

Session_OnEnd-This event occurs whenever the user ends the meeting. When a user finishes a meeting, the page does not require the user to specify the time (by default, this is 20 minutes).

Application_OnEnd-The last user after this event has ended the meeting. Typically, the Web server stops when this event occurs. This program is used to set up the cleanup after stopping the application, such as deleting records or writing data to text files.

A Global.asa file might 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: Because we cannot use ASP script delimited "(<% and%") inserts script in the Global.asa file, we handle the HTML <script> factor within the program.


-------------------------------------------------- ------------------------------

<object> statement
It is possible to create objects in the meeting or scope of application Global.asa using <OBJECT> tags.

Note: <OBJECT> tags should be outside the <script> mark!

Grammar

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)
Id Specifies a unique ID for the object
ProgID An ID associated with a class ID. The format for ProgID is [vendor.] component[. Version]

Either ProgID or ClassID must be specified.

ClassID Specifies a unique ID for a COM class object.

Either ProgID or ClassID must be specified

Example
The first example creates an object-scoped ProgID parameter used by the meeting name "Myad":

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

The second example creates an object's scope of application named "MyConnection" using the ClassID parameter:

<object runat= "Server" scope= "Application id=" myconnection "
classid=" Clsid : 8ad3067a-b3fc-11cf-a560-00a0c9081c21 ">
</object>
       

The declared object can be used in any script application in the Global.asa file:

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:

SOME. ASP FILE:

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

TypeLibrary statement
A typelibrary is a container for the content DLL file corresponding to the COM object. Including calls to TypeLibrary in Global.asa files, constants of COM objects can be accessed and errors can be better reported by ASP code. If your Web application relies on the type library of the declared data type of the COM object, you can declare the type library Global.asa.

Grammar

<!--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 is 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 isn't found, then the most recent version is used
Lcid Optional. The locale identifier to is used for the type library

Error value
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: Meta data labels can appear in Global.asa files (including internal and external <script> tags). However, it is recommended that the META tag display the Global.asa file near the top.


-------------------------------------------------- ------------------------------

Limit
Limit what you can do in the Global.asa file:

You can not display text, write in Global.asa file. Information that cannot be displayed by this file
You can only use the objects Application_OnStart and Application_OnEnd subroutines in the server and application. In the Session_OnEnd program, you can use the server, the application, and the Meeting object. In the Session_OnStart program you can use any of the built-in objects

-------------------------------------------------- ------------------------------

How to use subroutines
Global.asa is often used to initialize variables.

The following example shows how to detect the exact time a visitor first comes to the site. The time is stored in a session variable named "Start", and the value of the "START" variable can be applied from any ASP Web page:

<script language= "VBScript" runat= "Server" >
sub Session_OnStart session
("Started") =now () End
Sub
</script>
             

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

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

<script language= "VBScript" runat= "Server" >
sub Session_OnStart
Response.Redirect ("newpage.asp") End
Sub
</script>
               

You can include functional global.asa files.

In the following example, the Web server starts when the Application_OnStart program occurs.

Then application_onstart the alternate program called "GetCustomers". The "GetCustomers"

Subroutine to open a database and retrieve the records of the Customers table. The record is assigned to an array that can query the database from any ASP Web page:

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

A case study of Global.asa
In this example, we will create a Global.asa file that counts the current number of visitors.

Application variable for Application_OnStart set "tourist" for 0 o'clock, server started
The Session_OnStart of the auxiliary program has a variable "tourist" whenever a new visitor arrives
The Session_OnEnd of the auxiliary program subtracts a "visitor" every time the SEED program triggers
Global.asa file:

<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 some of the current visitors in the ASP file:

 

  
                     
 reprint please indicate from www.111cn.net/asp/asp.html 
   

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.