|
Application
and Session Object
The Session and Application object intrinsically generate unique identifiers and can hold
variables that are available across Web pages on the site.
Application object
An application object maintains information for the entire application. This information is available to all
users connected to the server. An application in ASP is defined by the IIS directory structure. for simplicity, one can consider an application as one web site.
An application object is invoked when the first user makes a request from the web Server The events for this object are stored
within a special file named global.asa. This file essentially includes any scripts for the
application objects Onstart and OnEnd events. These events are invoked when the Application ( or Session) starts and ends, respectively.
Various types of files can exist within an IIS directory, like HTML pages , images, multimedia
content, ASP or ASA files and so on. However, only the ASP and ASA files and so on. However, only the ASP and ASA files invoke the application and session objects.
Scope of an Application Object
The most important feature of the application object is the capability to scope objects and variables beyond a single page.
A user invokes and Application object when a
request is made to the application for the first time since the Web server was last started The application object or variables associated with it ar then kept alive until all session time-out, or the web server is restarted.
Using the Application Object
The event and methods of the Application object provide the infrastructure
necessary to maintain information across the application that can be used to manage all user
within an application The following listing includes the events and methods
of the object:
Events
Application Onstart
Application onend
Methods
Application Lock
Application Unlock
The Application OnStart event is like the initial Load event of a visual basic
form. It can be used to initialize various variables that would be used across the application For
example, it can open a database connection and create a recordset from that database. this recordset (and the database connection) would be available to all the pages and
users. so it would not be required to open it multiple time, The code is
written in the following manner:
<SCRIPT LANGUAGE=VBScript RunAt=Server>
Sub
Application_Onstart
'Code to execute when the application is started
End Sub
</SCRIPT>
The application OnEnd event is similar to the unload event that is invoked just before a visual basic form is closed. One use would be to clean up all database connection and objects,
recordset, variables etc. The syntax for writing the actions of this event is as follows:
<SCRIPT LANGUAGE=VBScript RunAt=Server>
Sub
Application_OnEnd
'Code to execute when the application is started
End Sub
</SCRIPT>
The lock and Unlock methods of the application object are used to protect properties from corruption while they are being update. When an Application is locked, other methods or function will not be able to update the properties.
Hence, if two users try to update the object will prevent the data from being corrupted. The syntax for the two methods is:
Application.Lock
Application.Unlock
In the following example, the methods have been used in the application OnStart event to update a counter.
<SCRIPT LANGUAGE=VBScript RunAt=Server>
Sub
Application_Onstart
Application.Lock
Application("intCounter")=Application("intCounter")+ 1
Application.Unlock
End Sub
</SCRIPT>
Properties of the application Object
The facility to dynamically add properties to the application object is one of its most useful features. Properties added to the Application object are similar to global constants and variables in Visual
Basic. These properties are actually what we referred to as variables and are used to share information across
pages of share information across pages of the application. Properties are
added by assigning a value to them:
Application("PropertyName")= Value
The Application object can be used to share any kind of data or object reference. for example
it may be used to store the number of users currently accessing the web site or to open a database connection which can be used by all the pages.
Session Object
The session object streamlines management to a user as he or she navigates through an application. Like the application object, the session object and its properties are available across
pages, but unlike those of the application object, the properties are user specific. This is very useful in
the multi-user environment of a web server. A session for a user begins when the first request is made to the web server The session terminates after a predefined
period. Within this period, all properties defined for that session are available to the user on all the pages requested.
IIS creates a session object by writing a cookie to the client browser. This cookie stores all information associated with the current Session object. If IIS restarts, the session abandons or the browser does not allow cookies, the session object will
attempt to initialize on every request.
|