In the published series, we have discussed two ASP objects: Application object and Session object, so we can access the collections, methods, properties, and events provided by Application object and Session object. This section will study these two objects from the perspective of programming.
· Create an Application object when an ASP DLL is loaded and the first request to an ASP web page is responded. This object provides a storage place to store variables and objects available for all web pages opened by all visitors.
· When a visitor requests an ASP page from the site for the first time, create a Session object for him and remains valid until the default timeout period (or the timeout period determined by the script). The object provides a storage place for storing variables and objects only available to web pages opened by the visitor during the activity of the session.
relation
1. Overview of ASP Application Object Members
This section describes the collection, methods, and events of Application objects (The Application objects have no properties). The Session object (with properties) is explained in the next section. Then we will continue to explore the tasks accomplished with these objects, explaining in more detail how each member of each object works.
1. Collection of Application objects
The Application object provides two collections that can be used to access variables and objects stored in the global application space. The collection and description are as follows:
Collection and description of table Application objects
| gather | illustrate |
| Contents | There is no set of all variables (and their values) stored in the Application object defined using the <OBJECT> element. Includes references to Variant arrays and Variant type object instances |
| StaticObjects | A collection of all variables (and their values) stored in the Application object defined using the <OBJECT> element |
2. Methods of Application Objects
The Application object method allows deleting values in the global application space, controlling concurrent access to variables within that space. The methods and instructions are shown in the following table:
Methods and descriptions of table Application objects
| method | illustrate |
| Contents.Remove(variable_name) | Remove a variable named variable_name from the Application.Content collection |
| Contents.RemoveAll() | Remove all variables from the Application.Content collection |
| Lock() | Lock the Application object so that only the current ASP page can access the content. Used to ensure that concurrent operations performed by methods that allow two users to read and modify the value simultaneously do not corrupt content |
| Unlock() | Unlock ASP web pages on Application objects |
Note that variables cannot be deleted from the Application.StaticObjects collection during runtime.
3. Events of Application Objects
The Application object provides two events that are triggered when it starts and ends, as shown in the following table:
Events and descriptions of table Application objects
| event | illustrate |
| OnStart | Triggered when ASP starts, before the user requests the web page to be executed and before any user creates the Session object. Used to initialize variables, create objects, or run other code |
| OnEnd | Triggered when the ASP application ends. Occurs after the last user session has ended and all code in the OnEnd event of that session has been executed. At the end of it, all variables present in the application are cancelled |
ASP's Session Object Member Overview
This section outlines all members of a Session object.
1. Collection of Session objects
The Session object provides two sets that can be used to access variables and objects stored in the user's local session space. These collections and descriptions are shown in the following table:
Set and description of table Session objects
| gather | illustrate |
| Contents | A collection of all variables and their values stored in this particular Session object, and these variables and values are not defined using <OBJECT> elements. Includes references to Variant arrays and Variant type object instances |
| StaticObjects | A collection of all variables stored in this Session object defined by using the <OBJECT> element |
2. Characteristics of Session Objects
The Session object provides four properties. These properties and descriptions are shown in the following table:
Table 3-7 Properties and descriptions of Session objects
| property | illustrate |
| CodePage | Read/write. Integer. Defines the Code Page for displaying page content in the browser. Code pages are numeric values of a character set, and different code pages may be used in different languages and places. For example, ANSI code page 1252 is used in American English and most European languages. Code page 932 is used for Japanese characters |
| LCID | Read/write. Integer. Defines the page area ID (LCID) sent to the browser. LCID is an international standard abbreviation that uniquely identifies a region. For example, the currency symbol that defines the current region in 2057 is '&pound;'. LCID can also be used in FormatCurrency and other statements as long as there is an optional LCID parameter. LCID can also be set in the ASP processing instruction <%…%> and takes precedence over the settings in the LCID attribute of the session. This chapter provides a list of ASP processing instructions later |
| SessionID | Read-only. Long shape. Returns the session identifier for this session, which is generated by the server when the session is created. Only unique during the lifetime of the parent Application object, so it can be reused when a new application starts |
| Timeout | Read/write. Integer. Define a timeout period in minutes for this session. If the user does not refresh or request a web page during the timeout period, the session ends. You can modify it as needed in each web page. The default value is 10min. This time should be shorter on sites with high usage |
3. Methods of Session Objects
The Session object allows the removal of specified values from the user-level session space and terminates the session as needed. The methods and descriptions of the Seesion object are shown in the following table:
Methods and descriptions of table Session objects
| method | illustrate |
| Contents.Remove(variable_name) | Remove a variable named variable_name from the Session.Content collection |
| Contents.RemoveAll() | Remove all variables from the Session.Content collection |
| Abandon() | When the execution of the web page is completed, the current user session is ended and the current Session object is undocumented. But even after the method is called, the variables of the current session in the page can still be accessed. When the user requests the next page, a new session will be started and a new Session object will be created (if it exists) Note that variables cannot be deleted from the Session.StaticObjects collection during runtime. |
4. Events of Session Objects
The Session object provides two events that are triggered at startup and end, as shown in Table 3-9:
Table 3-9 Events and descriptions of Session objects
| event | illustrate |
| OnStart | Triggered when the ASP user session starts, before the user requested web page is executed. Used to initialize variables, create objects, or run other code. |
| OnEnd | Triggered when the ASP user session ends. Starting from the user's last page request to the application, the event is triggered if the predetermined session timeout period has been exceeded. When the session ends, cancel all variables in that session. This event is also triggered when using the Abandon method to end an ASP user session in the code |
Events using Application and Session
ASP's Application and Session objects reflect features-events that other ASP built-in objects do not have. However, as seen in the previous object member table, these are events that are related to the ASP session and the work of the application.
1. Event handlers for Application and Session
Whenever an application or session starts or ends, the ASP triggers an event. These events can be detected and answered by writing ordinary script code in a special file, called global.asa, located in the root directory of an application (for the default web site is /InetPub/WWWRoot directory, or as a folder defined as a real application). This file can contain one or more <OBJECT> elements of HTML that are used to create component instances to be used within the application or user session.
The following code is an example of the global.asa file. We only focus on the <OBJECT> element and those lines of code that start with the Set keyword:
| <!-- Declare instance of the ASPCounter component with application-level scope //--> <OBJECT ID=ASPCounter RUNAT=Server SCOPE=Application PROGID=MSWC.Counters> </OBJECT> <!-- Declare instance of the ASPContentLimk component with session-level scope //--> <OBJECT ID=ASPContentLink RUNAT=Server SCOPE=Session PROGID=MSWC.NextLink> </OBJECT> <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Application_onStart() 'Create an instance of an ADO Recordset with application-level scope Set Application(ADOConnection)= Server.CreateObject(ADODB.Connection) Dim varArray(3) 'Create a Variant array and fill it VarArray(0) = This is a VarArray(1) = Variant array VarArray(2) = stored in the VarArray(3) = Application object Application(Variant_Array) = varArray'Store it in the Application Application(Start_Time) = CStr(Now) 'Store the date/time as a string Application(Visit_Count) = 0 'Set Counter variable to zero End Sub Sub Application_onEnd() Set Application(ADOConnection) = Nothing End Sub Sub Sesson_onStart() 'Create an instance of the AdRotator component with session-level scope Set Session(ASPAdRotator) = Server.CreateObject(MSWC.AdRotator) Dim varArray(3) 'Create a Variant arry and fill it VarArray(0) = This is a VarArray(1) = Variant array VarArray(2) = stored in the VarArray(3) = Session object Session(Variant_Array) = varArray 'Store it in the Session Session(Start_Time) = CStr(Now) 'Store the date/time as a string 'We can access the contents of the Request and Response in a Session_onStart 'event handler for the page that initiated the session. This is the *only* 'place that the ASP page context is available like this. 'as an example, we can get the IP address of the user: Session(Your_IP_Address) = Request.ServerVariables(REMOTE_ADDR) Application.Lock intVisits = Application(Visit_Count) +1 Application(Visit_Count) = intVisits Application.Unlock End Sub Sub Session_onEnd() Set Session(ASPAdRotator) = Nothing End Sub </SCRIPT> |
Because this global.asa file is used in the example page in this chapter, it will be necessary to put the file in the root directory of the web site, or in a directory configured as a virtual application, and include the directory Other sample files.
Read and store values
Note the above example how to read Application and Session variables, the same way you take in a collection of Request and Response objects. Set the values of these variables:
| Application(variable_name) = variable_value Application(variable_name) = variable_array_variable_name Set Application(variable_name) = object_reference |
Get the values of these variables:
| variable_value = Application(variable_name) variant_array_variable = Application(variable_name) Set object_reference = Application(variable_name) |
Of course, the same approach can be taken for Session objects.
You can see how to lock and unlock the Application object when accessed from a Session event processor; when accessed from an ASP web page, the same processing is required. This is not required when accessing values in Application objects using code within Application events. This is because in any application there is only one instance of the Application object, and its event handler's code is only performed when there is no active user session.
You can also see how a basic user session counter is implemented. Here is an application-level variable Visit_count, which is automatically increased when a new session starts. Generally, it is not restricted to simply save values into Application or Session objects. For example, the web developer's web site has a corresponding global.asa file on http://webdev.wrox.co.uk. When a new session starts, the file is written to the corresponding database on the server. entries, data details are obtained from the Request.ServerVariables collection. This provides a basic method to count the number of visitors and collect some basic information about visitors.