Application

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Application Object

An application on the Web may be a group of ASP files. The ASP 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 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 changes will automatically be reflected on all pages.
Application variables 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("users")=1 End Sub </script>

You can access the value of an Application variable like this: <% Response.Write(Application("users")) %>
<% dim i For Each i in Application.Contents Response.Write(i & "<br />") Next %>

<% dim i dim j j=Application.Contents.Count For i=1 to j Response.Write(Application.Conte nts(i) & "<br />") Next %>

<% dim i For Each i in Application.StaticObjects Response.Write(i & "<br />") Next %>

Lock and Unlock


You can lock an application with the "Lock" method. When an application is locked, the users cannot change the Application variables .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 %>

The #include Directive


You can insert the content of one ASP file into another 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.

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>

wisdom.inc
"One should never increase, beyond what is necessary, the number of entities required to explain anything." time.inc <% 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 in an ASP page, place the #include directive inside comment tags:

<!--#include virtual="somefilename"--> or <!--#include file ="somefilename"--> Use the virtual keyword to indicate a path beginning with a virtual directory.
Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file.

the file extension ".inc" for included files. if a user tries to browse an INC file directly, its content will be displayed. If your included file contains confidential information or information you do not want any users to see, it is better to use an ASP extension. An included file can also include other files, and one ASP file can include the same file more than once.

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 <% fname="header.inc" %> <!--#include file="<%=fname%>"-->

You cannot open or close a script delimiter in an INC file. This script will not work: <% For i = 1 To n <!--#include file="count.inc"--> Next %>

But this script will work:


<% For i = 1 to n %> <!--#include file="count.inc" --> <% Next %>

The Global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.
All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.

The Global.asa file can contain only the following: Application events Session events <object> declarations TypeLibrary declarations Note: The Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa file.

You might also like