| << 8.2.3- Application Object Methods | Chapter8 | 8.3.0- What is a Session? >> |
Global.asa
Since we can now store objects and variables in an application-level scope, we need to have a file to store these declarations in. This file is global.asa. Each application can have only one global.asa, and it's placed in the virtual directory's root. In global.asa, you can include event handler scripts and declare objects that will have Session or Application scope. You can also store application-level variables and objects used by the application. The file has no display component, as it is not displayed to users.
Understanding the Structure of global.asa
If you are using VBScript, global.asa can contain only four event handling subroutines:
- Application_OnStart
- Application_OnEnd
- Session_OnStart
- Session_OnEnd
An example of the most basic global.asa file would be:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server" >
Sub Application_OnStart
'...your VBScript code here
End Sub
Sub Application_OnEnd
'...your VBScript code here
End Sub
Sub Session_OnStart
'...your VBScript code here
End Sub
Sub Session_OnEnd
'...your VBScript code here
End Sub
</SCRIPT>
Notice that we have to use the <SCRIPT> tag at the top of the page. Also as previously mentioned, there are no <%…%> blocks in the file, because in the global.asa file, all of the ASP script needs be enclosed in the <SCRIPT> block. Since these scripts will be running on the server rather than on the client's machine, we have to make sure that the RUNAT directive is included inside of the SCRIPT element, otherwise global.asa won't work correctly.
Application_OnStart
This event handler is run once, when the application starts. The application starts when the first visitor to the application calls the first .asp page. In this procedure, you should put any application initialization steps that need to be run before anyone can access the application. For example, you could store the database login information, which you will see in Chapters 12 , 13 , and 14 , in an application-level variable, so that all pages have easy access to it.
If you want to initialize an application-scoped variable in Application_OnStart, you'd
use:
Sub Application_OnStart
Application("YourVariable") = "SomeValue"
End Sub
Once this event handler is complete, Session_OnStart runs for the first session.
Application_OnEnd
An application ends immediately after the last active session within that application ends. This event handler runs when the application is unloaded. To unload the application manually, you can do this from the IIS MMC console by just opening the console, selecting the application you want to unload in the left hand pane, then right click on the application and select Properties. Under the Virtual Directory tab at the top, there is a button on the right hand side of the dialog box marked Unload. If you click this, the application will be unloaded. It may take a little time, but the way to tell when the application has finished unloading is to wait until the button is grayed out.
Application_OnEnd scripts are used for "cleaning up" settings after the Application stops. For example, you might want to insert code to delete unneeded database records or write information you want to keep to text files.
Declaring Objects in global.asa
You can declare application-scoped objects in global.asa. This will allow you to create one instance of an object and then use it on any page and in every session that accesses your web application. To declare an object in global.asa:
<OBJECT RUNAT=Server SCOPE=Scope ID=Application PROGID="progID">
</OBJECT>
Or if you are using the CLASSID of the object:
<OBJECT RUNAT=Server SCOPE=Scope ID=Application CLASSID="ClassID">
</OBJECT>
Important Note about Global.asa causing errors
Global.asa can be a real pain to novice and experienced developers alike. That is, it can cause errors in your script that are seemingly unconnected in anyway to global.asa. The problem is that only one global.asa can be read by ASP. Which global.asa is read will depend on which application was visited first. This won't be a problem if you have explicitly declared your application and provided you haven't nested your applications inside one another.
What kind of errors will you see? Well Object doesn't support this property or method: is one that should have you scrambling to find Windows Explorer, to check out whether you have declared your application or not, or whether you have nested one application inside of another. Think of it this way: if you declare an object in global.asa and for some reason, ASP doesn't process the global.asa, then any time you reference that object in your script, ASP will generate an error. Type Mismatch is another error caused for very similar reasons. If you declared a variable to hold an instance of an object in global.asa, and for some reason ASP is not reading the correct global.asa, then the ASP script could still be expecting you to read in a variable not an object.
If you think about it, this is perfectly reasonable, how can ASP know which global.asa to use if you nest applications or haven't declared your application, clairvoyance isn't one of things Microsoft have been able to program in yet. Only you know which global.asa should be read and therefore you have to make sure the applications are explicitly declared, and also not nested.
Extra Warnings on Declaring Objects in global.asa
You should be careful about creating components in application scope: some components are not designed to be given application scope, such as the Dictionary object. If you are not sure if an object can be used in application-scope, then you should err on the side of caution and find another place to store the object.
The use of objects in application or session scope is really an advanced topic that is beyond the scope of this book. There are advantages that can be gained by using them, but along with those advantages comes a host of possible problems. For a more detailed look at how to use objects in global.asa, take a look at Professional Active Server Pages 3.0, available from Wrox Press, ISBN 1-861002-61-0. (This book is no longer available from Wrox but you might find it from a store that sells used books.)
Next, we'll have a look at ASP's link between the Application and the client – the Session.
| << 8.2.3- Application Object Methods | Chapter8 | 8.3.0- What is a Session? >> |

RSS
