The ASP Application Object
By Stephen Bucaro
When you install Internet Information Server (IIS), the installer creates a default
Web site in a directory named wwwroot. You could start building your website
in the wwwroot directory, but if you are a Web host provider, you will want to
configure multiple websites to rent to different customers. You create websites
using the Internet Service Manager (ISM).
Each website will run in its own separate process, so if one website crashes, the
other websites will continue to run. Each website is an application associated with
an application object. You can create subdirectories within the website (or virtual
directories at any location) and use ISM to make them into applications. Then if
an application crashes, the website will continue to run.
A website can store data in named memory locations called "variables". Each variable
has a "scope" or level from which it is visible and accessible. For example, when a
variable is declared within a procedure or function, it is only visible to and
can be accessed by only code within that procedure or function. When processing
exits the procedure or function, a variable declared within is lost.
Variables can be declared on a webpage outside of any procedure or function. That
variable would have a "page" scope and would be visible to and can be accessed by
only code within that webpage. Variables can be declared at the application level.
Below is an example of how to declare an application level variable.
Application("variable_name") = "some text"
A variable declared at the application level is visible to and can be accessed by
all pages within that application. An "Application" object is associated with each
application and provides an event that is triggered when the application starts and
when the application ends. Active Server Pages (ASP) provides access to these events
through a special file named "global.asa". The contents of the global.asa file is
shown below.
<script language="VBScript" runat="server">
Sub Application_OnStart
End Sub
Sub Application_OnEnd
End Sub
</script>
The Application_OnStart and Application_OnEnd event handlers can be used
for code that needs to run when the first page of the application is served and when the
Website is shut down.
Scripts running at the application level can be accessed by all user sessions. The
Application object provides two methods we can use to protect shared variables from
being written to by more than one user at a time.
Lock restricts other code from writting
Unlock releases the variable for writting
For example, if you wanted to keep a count of the number of pages served by an application,
in the global.asa file you could declare an application level variable hits as shown below.
<script language=VBScript runat=server>
Sub Application_OnStart
Application.Lock
Application("hits") = 0
Apllication.Unlock
End Sub
</script>
|