Page 237 - DCAP408_WEB_PROGRAMMING
P. 237
Unit 9: ASP Objects
a good idea to create a single database connection object and have all users communicate to a Notes
database through that object. However, as with most other objects, it is always best to wait to
create the object until you need it.
Task The ADO Connection object will degrade your server’s performance if put into the
Application object. Analyze
Like the Session object, the Application object is easy to use, and the temptation to create a
plethora of application variables is high indeed. Many developers use the Application object to
store many Web site-wide constants - for example, a navigational footer common to all Web
pages, or perhaps the Webmaster’s email address. Although it’s better to place these items in the
Application object than in the Session object, they belong best in a static text file on the Web
server. This text file can then be included into any ASP page that needs to display the navigation
footer or the Webmaster’s email address. We will discuss how to include files on Day 13.
Sometimes, however, the use of application variables is preferred to include files. If the data you
need to store changes often, such as the last post to a message board, then the information should
be stored in the Application object. Include files should only be used if the data is static and not
susceptible to frequent change.
Although you can afford to be less prudent when creating application variables than you can be
when creating session variables, you should still strive to use the minimum needed amount of
such variables. Because the Application object persists in the Web server’s memory, the fewer
the application variables stored within it, the less drain on the Web server’s performance.
Therefore, the Application should remain free of objects and contain only needed application
variables.
Initializing Application and Session Variables
Recall that the Session and Application objects serve as warehouses for session and application
variables, respectively. When the user first visits the site, a new Session warehouse is created.
What is inside this warehouse by default? Initially, the warehouse is empty. Attempting to
retrieve a nonexistent session variable from the users Session returns an empty string.
However, the Session and Application objects both have an event you can use to initialize the
contents of your users Session and your Website’s Application. This event is called the OnStart
event, and it occurs at different times for the Session and Application objects. The Session’s
OnStart event occurs whenever a new user comes to the site. This is when a new Session object
instance is created for this particular user. In the OnStart event, you can create the session
variables you plan to use and initialize them to certain values. The Application’s OnStart event
fires when the first Session object is instantiated - that is, when the first Session’s OnStart event
fires.
To create and initialize session and application variables, you need to write event handlers for
the OnStart events. Listing 11.13 displays the OnStart event handler for both the Session and
Application objects.
Listing - Initializing Application and Session Variables in the OnStart Events
1: Sub Application_OnStart()
2: Application(“LastPost”) = “”
3: End Sub
LOVELY PROFESSIONAL UNIVERSITY 231