(5 ratings)   
By: msconline.maconstate.edu
Among the various ASP built-in objects, we've had occasion to discuss and use the Server Object (to create instances of other objects), the Response Object (to output script processing to a Web page), the Request Object (to handle script input from...
Added: 25 June 2008    Views: 1341  
PathComputers    Programming    Asp
Keywords: computers   programming   language   asp   database   functions   code   coder  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

Among the various ASP built-in objects, we've had occasion to discuss and use the Server Object (to create instances of other objects), the Response Object (to output script processing to a Web page), the Request Object (to handle script input from forms and query strings), and the Session Object (to server as a global repository for user sessions). As we proceed through this tutorial there will be occasion to discuss other features associated with these objects.

The final built-in ASP object is the Application Object. It works pretty much like the Session Object, being a global storage and scripting area. However, whereas the Session Object pertains to individual user sessions, the Application Object pertains to the Web site as a whole.

One of the primary uses of the Application Object is to store information that needs to be accessible by all pages and by all visitors. In other words, it is a global area for the Web application itself, not just for individual visitors. Therefore, any information stored in the Application Object is accessible at any time by anyone.

The global.asa File

Scripts interact with the Application Object through the global.asa file. In addition to the Session OnStart and OnEnd subroutines, the global.asa file can contain subroutines to trigger processing related to the start-up and shut-down of a Web site.

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Sub Application_OnStart
  ... script to run when a Web site begins
End Sub

Sub Application_OnEnd
  ... script to run when a Web site ends
End Sub


Sub Session_OnStart
  ... script to run when a visitor arrives
End Sub

Sub Session_OnEnd
  ... script to run when a visitor leaves
End Sub

</SCRIPT>

The Application_OnStart subroutine defines variables and/or runs scripts when the Web application starts up. This happens the first time the first visitor arrives at the site. It also happens when Internet services are restarted after being shut down. The Application_OnEnd subroutine runs when Internet services are shut down or the last visitor leaves the site.

Defining Application-Wide Variables

The important point about the Application_OnStart subroutine is that you can define global variables that are accessible at all times by all pages. The following is a situation ready-made to take advantage of this feature.

By now you are probably irritated with having to enter the following line of code over and over again:

CNObj.Open "DBQ="d:\Databases\Database.mdb;DRIVER=Microsoft Access Driver (*.mdb)

This statement, of course, is needed to link your Connection Object with your database. Not only is the typing tedious (or perhaps you just copy and paste it), but what happens if you decide to change your directory structure, or move your database, or rename it? You will need to go through all of your scripts and change all the references, and risk missing a script or mistyping the references. This is not an easy task, especially when your site has grown to dozens and dozens of pages.

The Application Object, fortunately, provides a very easy way around these problems. You can define a variable within the global.asa file containing the current connection string. This variable is defined when the application starts up, making it accessible to all pages for all visitors. Then, rather than hard coding the path to the database when a connection is established, you reference the global variable containing the connection string. If you ever need to change the location or name of your database, you only need to change the connection string once, in the global.asa file.

Here's how this would work for our logon application. First, an Application_OnStart subroutine is added to the global.asa file within which we define the current path to our database. This path string is assigned to the Application variable Application("DB"). Any application-wide variable must use the naming convention Application("variable").

script start here


Sub Application_OnStart


  Application("DB")="DBQ=d:\Databases\Database.mdb;DRIVER=Microsoft Access Driver (*.mdb)"


End Sub


Sub Session_OnStart


  Session("PassCheck") = False


  '-- Add 1 to the Visitor Counter
  Set CNObj=Server.CreateObject("ADODB.Connection")
  CNObj.Open "DBQ=d:\Databases\Database.mdb;DRIVER=Microsoft Access Driver (*.mdb)"
  CNObj.Execute "UPDATE Counters SET VisitorCounter = VisitorCounter + 1"
  CNObj.Close


End Sub


script end here

Once Application("DB") is defined and becomes site-accessible through the startup subroutine, this variable name can be used in all scripts in place of the literal connection string. So, any time we need to connect to our database, we can use the statements:

Set CNObj=Server.CreateObject("ADODB.Connection")
CNObj.Open Application("DB")

Plus, if we need to change the location of our database, or even if we change the DBMS itself, the connection string in the global.asa file is all that needs to be changed. All other scripts are unaffected.

In the following tutorials we not be using this technique, just to make the connection string more obvious in the examples. But you are greatly encouraged to apply it in your own sites.

About the Author :
msconline.maconstate.edu
Using Application Variables
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments