Article

ASP Sessions and Applications

Page: 1 2 3 4 5 6 Next

The global.asa File

global.asa is a special file that you can create in the root directory of your Web application. It specifies the actions to be taken in response to four events:

  • Application start (Application_OnStart)

  • Application end (Application_OnEnd)

  • Session start (Session_OnStart)

  • Session end (Session_OnEnd)

    Application_OnStart can be used to set up initial values for application variables, or perhaps to restore some saved information from the last time the Web server was run, by loading it from a file or database. Application_OnEnd, meanwhile, is generally used to clean up any open resources, and save information that will be needed the next time the Web application is started. Session_OnStart and Session_OnEnd perform similar functions for user sessions.

    Let's create a simple global.asa file to see these events in action. Open Notepad (or your text editor of choice) and create a file called global.asa in the directory that houses your Web application. Type the following as the contents of this file:

    1  <script language="VBScript" runat="Server">    
    2  Sub Application_OnStart    
    3    ' Commands to be run at startup    
    4  End Sub    
    5    
    6  Sub Application_OnEnd    
    7    ' Commands to be run at shutdown    
    8  End Sub    
    9    
    10 Sub Session_OnStart    
    11   ' Commands to be run at session start    
    12 End Sub    
    13    
    14 Sub Session_OnEnd    
    15   ' Commands to be run at session end    
    16 End Sub    
    17 </script>

    This is the basic structure of a global.asa file. Let's say, just for fun, that we wanted to start the count of our global (Application) hit counter at 1000. The code for the /Application_OnStart section (lines 2-4) might look like this:

    2  Sub Application_OnStart    
    3    Application("hits") = 1000  ' Pad the numbers :)    
    4  End Sub

    In a more practical example, you might use Application_OnEnd to save the current hit count into a text file when the server is shut down, and Application_OnStart to load that value when the server starts up again.

    If you liked this article, share the love:
    Print-Friendly Version Suggest an Article
  • Sponsored Links