|
|
Application and Session Objects
5/27/2003 10:44:02 AM - Kenneth Richards
Two very important features of the ASP environment are the Application and Session objects. These built-in ASP objects are available for your use in any of your scripts and will help you to maintain state across multiple ASP pages.
Global Application Settings
The most common use of the application object is to store global application settings. You can think of the Application object as a hash table. A hash table is basically helps you map one string to another. As an example, you could map each month to the number of days in the month (ie: "January" = 31)
Below is an example of how assigning and retrieving an application variables is done.
Application("SiteName") = "Bob's Bait and Tackle" Response.Write "Welcome to " & Application("SiteName")
Actually, you can do more than this. The value (shown on the right-hand side) can be any type supported by ASP (even an array of values.) The one limitation you have is that you cannot assign an object like a database connection object or a COM+ object that you have created.
When you are setting application variables, you can lock the application object for exclusive use to make sure that all of your changes are done before any other process makes modifications to the application hash. This is done with the Lock and Unlock methods.
Application.Lock Application("Today") = Now() Application("MOTD") = "Happy Easter!" Application.Unlock
Similarily, you can remove items from the Application object by using the Remove method. Using this method, you can remove a single item at once. You can also iterate over the entire collection of application variables using the method shown below. Also, you can remove every single variable using the RemoveAll method.
' iterate over all of the application variables Dim sKey
For Each sKey In Application.Contents Response.Write sKey & " = " & Application(sKey) & "<BR>" Next
' remove a single item from the application hash Application.Remove("Today")
' remove all elements from the application hash Application.RemoveAll
Once an application variable (string association) has been defined, it will be available to all scripts for your web application. If you want to define global web site variables, you would use the application object. To make sure they are always available to all of your scripts, you would place these definitions in your global.asa file. This special file holds ASP code that will be executed whenever the web server service starts or stops.
Event Handlers
With the global.asa file, you can define special event handlers that will automatically be invoked when certain events occur. These events are the starting and stopping of the web server and when a new session instance is created or destroyed.
You can think of a session as a "login session". As soon as a new user comes to your site, a new session is created. When the user leaves, their session is destroyed. Of course, users are not actually logging onto your web site and since HTTP doesn't maintain a connection with the server, the web server has to "guess" when the user has left the web site by "timing-out" the session after a specific length of time has elapsed without any activity.
' global.asa sample file
' event - web server service started Sub Application_OnStart ' set your global variables here Application("SiteName") = "Bob's Bait and Tackle" Application("SiteRoot") = "http://www.bobsbaitandtackle.com" End Sub
' event - web server service stopped Sub Application_OnEnd ' perform any clean-up operations here End Sub
' event - new user session created (new visitor) Sub Session_OnStart ' assign any session specific information Randomize Session("UserID") = Int(Rand() * 10000 + 1) End Sub
' event - user session destroyed (timed-out) Sub Session_OnEnd ' perform any clean-up for the session here End Sub
Session Object
The session object is another hash table that stores string-to-variant associations. This relationship is often referred to as a key-to-value relationship since the first string is the "key" to retrieving the value. Without it, there is virtually no way to recover the value.
The session object should be thought of as a collection of objects. Each unique visitor to your site gets their very own instance (or copy) of the session object. This copy is initally blank, but hash entries can be added through your Session_OnStart event handler or though any of your ASP pages.
Possible uses for the session object are: storing web site preferences for a visitor, managing a shopping cart and managing user logins (creating protected content for your site.) Basically, any information that is specific for a user and you want to save between different page views is a candidate for the session object.
You cannot reference the Session object inside of the global.asa or in the Application event handlers. Other than that, you can access the session objects anywhere. The same restrictions that apply to the application object, also apply to the session object. Any ASP value may be assigned to a session hash except objects or COM+ objects.
The methods for iterating over the session collection and removing hash entries is nearly identical to the Application object.
' iterate over all of the session variables Dim sKey
For Each sKey In Session.Contents Response.Write sKey & " = " & Session(sKey) & "<BR>" Next
' remove a single item from the session hash Session.Remove("Today")
' remove all elements from the session hash Session.RemoveAll
Please note that in order to use the Session object, you need to make sure that this is enabled in your IIS (Internet Information Server) cofiguration. By default, this option is enabled, but your server administrator may have disabled it to improve performance. Tracking session creates a bit of an overhead on your web server so many people choose to disable or not even use it.
Your alternatives to using the session object are using cookies or passing along session information in the querystring. We won't cover these alternative here in this article, but will instead write another article dedicated to this topic in the near future.
Conclusion
It is a good idea to keep commonly used information in your application object. This becomes especially useful when you are developing more than one web site. Each web page becomes like a template which you can just copy over to another site and use it without any modification.
Another very useful use of the Application object is to cache dynamic content that is pulled from a database. This can drastically speed up the performance of your web server and allow you to handle more simultaneous visitors to your site. This too will be covered in a future article.
|
 |

|
|
| |
| "I only regret that I have one life to lose for my country." | - Nathan Hale |
©2010 San Diego Web Design - Orvado Technologies, All Rights Reserved
ASP Nut provides articles and reference documentation for Active Server Page developers.
|
|
|