2025-04-27 07:49:33 -04:00

224 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<TITLE>Application Object</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Window_OnLoad()
Dim frmContents
On Error Resume Next
If Not Parent Is Nothing Then
Set frmContents = Parent.Contents
If Not frmContents Is Nothing Then
frmContents.Window.TOCSynch_Click
End If
End If
End Sub
//--></SCRIPT><META NAME="DESCRIPTION" CONTENT="Internet Information Services reference information">
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="MS-IT-LOC" Content="Internet Information Services">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><A NAME="_application_object"></A>Application Object</H2>
<P>You can use the <B>Application</B> object to share information among all users of a given application. An ASP-based application is defined as all the .asp files in a virtual directory and its subdirectories. Because the <B>Application</B> object can be shared by more than one user, there are <B>Lock</B> and <B>Unlock </B>methods to ensure that multiple users do not try to alter a property simultaneously.</P>
<H6>Syntax</H6>
<P><B>Application.</B><I>method</I> </P>
<P>&nbsp;</P>
<H6>Collections</H6>
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD width=24%><A HREF="/iishelp/iis/htm/asp/vbob129a.htm"><B>Application Contents Collection</B></A></TD>
<TD width=76%>Contains all of the items that have been added to the application through script commands.</TD>
</TR>
<TR VALIGN="top">
<TD width=24%><A HREF="/iishelp/iis/htm/asp/vbob5uem.htm"><B>Application StaticObjects Collection</B></A></TD>
<TD width=76%>Contains all of the objects added to the session with the &lt;OBJECT&gt; tag. </TD>
</TR>
</TABLE><BR>
<H6>Methods</H6>
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob60kw.htm"><B>Contents.Remove</B></A></TD>
<TD width=75%>The <A HREF="/iishelp/iis/htm/asp/vbob60kw.htm"><B>Contents.Remove</B></A> method deletes an item from the <B>Application</B> object's <B>Contents</B> collection.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob1wxc.htm"><B>Contents.RemoveAll</B></A></TD>
<TD width=75%>The <A HREF="/iishelp/iis/htm/asp/vbob1wxc.htm"><B>Contents.RemoveAll</B></A> method deletes all items from the <B>Application</B> object's <B>Contents</B> collection.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob84h7.htm"><B>Lock</B></A></TD>
<TD width=75%>The <B>Lock </B>method prevents other clients from modifying <B>Application </B>object properties.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob51gr.htm"><B>Unlock</B></A></TD>
<TD width=75%>The <B>Unlock </B>method allows other clients to modify <B>Application </B>object properties.</TD>
</TR>
</TABLE><BR>
<H6>Events</H6>
<TABLE border=0 cellpadding=5 cols=1 frame=box rules=rows>
<TR VALIGN="top">
<TD width=100%><A HREF="/iishelp/iis/htm/asp/glob6zc4.htm"><B>Application_OnEnd</B></A></TD>
</TR>
<TR VALIGN="top">
<TD width=100%><A HREF="/iishelp/iis/htm/asp/glob4f78.htm"><B>Application_OnStart</B></A></TD>
</TR>
</TABLE><BR>
<P>Scripts for the preceding events are declared in the Global.asa file. For more information about these events and the Global.asa file, see the <A HREF="/iishelp/iis/htm/asp/glob63vp.htm"><B>Global.asa Reference</B></A>.</P>
<H6>Remarks</H6>
<P>You can store values in the <A HREF="/iishelp/iis/htm/asp/vbob3z77.htm"><B>Application Collections</B></A>. Information stored in the <B>Application </B>collections<B> </B>is available throughout the application and has application scope. The following script demonstrates storage of two types of variables.</P>
<PRE><CODE>&lt;%
Application("greeting") = "Welcome to My Web World!"
Application("num") = 25
%&gt;
</CODE></PRE>
<P>Each of these variables would be members of the <A HREF="/iishelp/iis/htm/asp/vbob129a.htm"><B>Application Contents Collection</B></A>.</P>
<P>You can also assign a component instance to a variable that has application scope. If you assign a component instance to a variable with the <B>Server.CreateObject</B> method, the variable will be a member of the <B>Application.Contents</B> collection. If the variable is assigned with the &lt;OBJECT&gt; tag, the variable will be a member of the <A HREF="/iishelp/iis/htm/asp/vbob5uem.htm"><B>Application StaticObjects Collection</B></A>.</P>
<P>You should be careful about assigning component instances to variables with application scope, because some components are not designed to be given application scope. For more information, see the Platform SDK.</P>
<P>If you assign a component instance to a variable in the <A HREF="/iishelp/iis/htm/asp/vbob129a.htm"><B>Application Contents Collection</B></A>, and use Visual Basic<sup>&reg;</sup> Scripting Edition (VBScript) as your primary scripting language, you must use the <B>Set </B>keyword. This is illustrated in the following script.</P>
<PRE><CODE>&lt;% Set Application("Obj1") = Server.CreateObject("MyComponent") %&gt;
</CODE></PRE>
<P>You can then reference the methods and properties of <CODE>MyComponent</CODE> on subsequent Web pages by using this script</P>
<PRE><CODE>&lt;% Application("Obj1").MyObjMethod %&gt;
</CODE></PRE>
<P>or by extracting a local copy of the object and using the following</P>
<PRE><CODE>&lt;%
Set MyLocalObj1 = Application("Obj1")
MyLocalObj1.MyObjMethod
%&gt;
</CODE></PRE>
<P>Another way to create objects with application scope is by using the &lt;OBJECT&gt; tag in the Global.asa file. For more information, see the <A HREF="/iishelp/iis/htm/asp/glob63vp.htm"><B>Global.asa Reference</B></A>.</P>
<P>You cannot store a built-in object in the <B>Application </B>object. For example, each of the following lines returns an error.</P>
<PRE><CODE>&lt;%
Set Application("var1") = Session
Set Application("var2") = Request
Set Application("var3") = Response
Set Application("var4") = Server
Set Application("var5") = Application
Set Application("var6") = ObjectContext
%&gt;
</CODE></PRE>
<P>You should be aware of the threading model used by any components you give application scope. The threading model used to develop the component will have a significant impact on whether a component instance should be assigned to a variable in one of the <B>Application </B>collections. </P>
<P>If you store an array in an <B>Application </B>object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:</P>
<PRE><CODE>&lt;% Application("StoredArray")(3) = "new value" %&gt;
</CODE></PRE>
<P>This is because the <B>Application </B>object is implemented as a collection. The array element <CODE>StoredArray(3)</CODE> does not receive the new value. Instead, the value would be included in the <B>Application</B> object collection, and would overwrite any information that had previously been stored at that location.</P>
<P>It is strongly recommended that if you store an array in the <B>Application </B>object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the <B>Application </B>object again, so that any changes you made are saved. This is demonstrated in the following scripts.</P>
<PRE><CODE>---file1.asp---
&lt;%
'Creating and initializing the array.
dim MyArray()
Redim MyArray(5)
MyArray(0) = "hello"
MyArray(1) = "some other string"
'Storing the array in the Application object.
Application.Lock
Application("StoredArray") = MyArray
Application.Unlock
Server.Transfer("file2.asp")
%&gt;
---file2.asp---
&lt;%
'Retrieving the array from the Application Object
'and modifying its second element.
LocalArray = Application("StoredArray")
LocalArray(1) = " there"
'Printing out the string "hello there."
Response.Write(LocalArray(0)&amp;LocalArray(1))
'Re-storing the array in the Application object.
'This overwrites the values in StoredArray with the new values.
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock
%&gt;
</CODE></PRE>
<H6>Example</H6>
<P>The following example uses the application variable <I>NumVisits </I>to store the number of times that a particular page has been accessed. The <B>Lock</B> method is called to ensure that only the current client can access or alter <CODE>NumVisits</CODE>. Calling the <B>Unlock</B> method then enables other users to access the <B>Application</B> object.</P>
<PRE><CODE>&lt;%
Application.Lock
Application("NumVisits") = Application("NumVisits") + 1
Application.Unlock
%&gt;
This application page has been visited
&lt;%= Application("NumVisits") %&gt; times!
</CODE></PRE>
<P>You can see a working sample .asp file that uses the <B>Application</B> object in the Building ASP Applications section of the <b>ASP Samples</b> in the IIS section of the Platform SDK or at <a href="http://go.microsoft.com/fwlink/?LinkId=1753" target="_blank">MSDN Online - Internet Information Services</a>.</P>
<hr class="iis" size="1">
<p align="center"><em><a href="../../../common/colegal.htm">&copy; 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
</BODY>
</HTML>