Wednesday, December 22, 2010

Special ASP File Global.asa

An optional file that can contain declarations of objects, variables, and methods that can be accessed by any ASP page in an application. This file is stored in the root folder of your ASP application and must be named GLOBAL.ASA.An application can only have one Global.asa file.
Global.asa files can contain only the following:

  •  ASP Built-in Object Events 
  •  OBJECT Declarations
  •  TypeLibrary Declarations

The scripts contained in the Global.asa file may be written in any supported scripting language. If multiple event or object scripts use the same scripting language, they can be combined inside a single set of script
tags.

When we save changes to the Global.asa file, the server finishes processing all of the current application requests before it recompiles the Global.asa file. During that time, the server refuses additional requests and returns an error message stating that the request cannot be processed while the application is being restarted.


After all of the current user requests have been processed, the server deletes all active sessions, calling theSession_OnEnd event for each session it deletes, closes the application, and calls the Application_OnEnd event. The Global.asa file is then recompiled. Subsequent user requests will start the application and create new sessions, and trigger the Application_OnStart and Session_OnStart events.


Procedures declared in the Global.asa file can be called only from one or more of the scripts associated with theApplication_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. They are not available to the ASP pages in the ASP-based application.


ASP Built-in Object Events 


• Application_OnStart – This event is called the FIRST time any user hits an ASP page in the same tree as the GLOBAL.ASA. The first hit of any user is considered to be when the application is first invoked. This function is good for loading any default values for application variables, and initializing logs files and such. The application state is reset after the Web Server is restarted, or when the GLOBAL.ASA file is modified. The Session_OnStart will automatically fire when this event finishes.

• Session_OnStart – This event is called the first time each new user hits the ASP application. So, for each session that is created, this event is called. This event is good for setting up any session level defaults or variables that will be needed for the duration of the user session.

• Session_OnEnd – This even will fire when a user ends a session. This can be done by a user logging out manually, or via a specified timeout period (by default 20 minutes). Useful for session cleanup code.

• Application_OnEnd – This final event occurs when the last user ends his or her session. This process normally fires when the web server is being shut down. This is useful for closing files, creating reports, and updating certain usage statistics.

Here is an example of using the GLOBAL.ASA to create a simple hit counter:



<SCRIPT language=vbscript runat="server">

Sub Application_OnStart
  getcounter
End Sub

Sub Session_OnStart
  Application
.Lock
  Application
("currentCount")=Application("CurrentCount")+1
  Application
.UnLock
End Sub

Sub Application_OnEnd
  Writecounter
End Sub

sub getcounter
  set conn
=Server.CreateObject("ADODB.Connection")
  
conn.Provider="Microsoft.Jet.OLEDB.4.0"
  
conn.Open "c:/webdata/counter.mdb"
  
set rs=conn.execute("select currentcount from datatable”)
  Application("
currentCount")=rs(“currentcount”)
  rs.Close
  conn.Close
end sub

sub writecounter
  set conn=Server.CreateObject("
ADODB.Connection")
  conn.Provider="
Microsoft.Jet.OLEDB.4.0"
  conn.Open "
c:/webdata/counter.mdb"
  conn.execute("
update datatable set currentcount “ &  
    
application(“currentcount”))    
  
conn.Close
end sub

This my web page There have been <%response.write(Application("currCount"))%>
Visitors to this website!


No comments:

Post a Comment