ASP
Application Object
A
group of ASP files that work together to
perform some purpose is called an application.
The Application object in ASP is used to tie
these files together.
Application
Object
An
application on the Web is a group of asp
files. The files work together to perform some
purpose. The Application object in ASP is used
to tie these files together.
The
Application object is used to store variables
and access variables from any page, just like
the Session object. The difference is that all
users share ONE Application object, while with
Sessions there is one Session object for each
user.
The
Application object should hold information
that will be used by many pages in the
application (like database connection
information). This means that you can access
the information from any page. It also means
that you can change the information in one
place and the new information will
automatically be reflected on all pages.
Store
and Retrieve Variable Values
Application
variables must be created in the Global.asa
file, but they can be accessed and changed by
any page in the application.
You
can create Application variables in Global.asa
like this:
<script
language="vbscript" runat="server">
Sub Application_OnStart
application("vartime")=""
application("whoon")=1
End Sub
</script> |
In
the example above we have created two
Application variables: The first is named
vartime, and the second is named whoon.
You
can access the value of an Application
variable like this:
There
are
<%
Response.Write(Application("whoon"))
%>
active connections. |
Looping
Through the Contents
You
can loop through the "Contents"
collection, to see the values of all the
Application variables:
<%
dim i
For Each i in Application.Contents
Response.Write(Application.Contents(i)
& "<br>")
Next
%> |
If
you don't know how many items are stored in a
"Contents" collection, you can use
the "Count" property:
<%
dim i
dim j
j=Application.Contents.Count
For i=1 to j
Response.Write(Application.Contents(i)
& "<br>")
Next
%> |
Looping
Through the Objects
You
can loop through the "StaticObjects"
collection, to see the values of all the
objects stored in the Application Object:
<%
dim i
For Each i in
Application.StaticObjects
Response.Write(Application.StaticObjects(i)
& "<br>")
Next
%> |
Lock
and Unlock
You
can lock an application with the
"Lock" method. When an application
is locked, the users can not change the
Application variables (other than the one
currently accessing it). You can unlock an
application with the "Unlock"
method. This method removes the lock from the
Application variable:
<%
Application.Lock
'do some application object
operations
Application.Unlock
%> |
ASP
Including Files
The
#include directive is used to create
functions, headers, footers, or elements that
will be reused on multiple pages.
The
#include Directive
It
is possible to insert the content of another
file into an ASP file before the server
executes it, with the #include directive. The
#include directive is used to create
functions, headers, footers, or elements that
will be reused on multiple pages.
How
to Use the #include Directive
Here
is a file called "mypage.asp":
<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"--></p>
</body>
</html>
|
Here is the "wisdom.inc" file:
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
|
Here is the "time.inc" file:
<%
Response.Write(Time)
%>
|
If you look at the source code in a browser, it will look something like this:
<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is necessary,
the number of entities required to explain anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>
|
Syntax
for Including Files
To include a
file into an ASP page, place the #include
directive and the virtual or file keyword
inside comment tags:
| <!--#include
virtual="somefilename"-->
or
<!--#include
file ="somefilename"--> |
"Somefilename"
is the name of the file you want to include.
Using
the Virtual Keyword
The
"virtual" keyword allows you to
include files from another virtual directory
under the same web server.
As an
example: if a file named "header.inc"
resides in the root directory, the following
line inserts the contents of "header.inc"
into an ASP file:
| <!--#include
virtual ="/header.inc"--> |
Note: The
virtual keyword should be used when you want
to use the include files on several
pages in different web sites under the same
web server.
Using
the File Keyword
The
"file" keyword allows you to include
files from the same directory as the including
page or from that directory's subdirectories.
As an
example: if an ASP file resides in the same
directory as a file named "header.inc,"
it can insert the contents of "header.inc"
like this:
<!--#include file ="header.inc"-->
|
Note: The
file keyword should be used when you want to
use the include files on several pages within
one web site.
Differences
Between the File and Virtual Keyword
<!--#include virtual="/asp/header.inc"-->
|
The line
above includes a file named "header.inc"
from asp's directory, even if the file with
this statement is in the /ado/text folder.
<!--#include file="/asp/header.inc"-->
|
The line
above will fail from ado's directory.
| <!--#include
file="../asp/header.inc"--> |
The line
above will succeed from ado's directory.
Tips
and Notes
In the
sections above we have used the file extension
".inc" for the included files.
Notice that if a user tries to browse an
".inc" file directly, its content
will be displayed. So if your included file
contains source code you do not want any users
to see, it is better to use an
".asp" extension. The source code in
an ".asp" file will not be visible
after the interpretation.
An included
file can include other files, and one ASP file
can include the same file more than once.
Important:
Included files are processed and inserted
before the scripts are executed.
The following
script will not work because ASP executes the
#include directive before it assigns a value
to the variable:
|