This section describes the two objects of application and session from the perspective of program design.
· When loading asp dll and responding to the first request to an asp web page, create an application object. This object provides a storage space for storing variables and objects available for all webpages opened by all visitors.
· When a visitor requests an ASP page from the site for the first time, a session object is created for the visitor and remains valid until the default timeout period (or the timeout period determined by the script ). This object provides a storage space for storing the variables and objects available only for the webpage opened by the visitor during the activity of the session.
Figure 3-12 (Figure 1-20) shows the distribution of user requests and server responses in sessions. All sessions are in ASP applications.
3.3.1 ASP Application Object member Overview
This section describes the set, method, and event of the Application Object (the application object has no attributes ). In the next section, the session object (with attributes) is described in the same way. Then we will continue to explore the tasks completed by using these objects and explain in detail how each member of each object works.
1. Set of application objects
The Application Object provides two sets for accessing the variables and objects stored in the global application space. Table 3-3 shows the set and description:
Table 3-3 set and description of application objects
Integration
Description
Contents
There is no set of all variables (and their values) stored in the Application object defined by elements. Including the variant array and the reference of the variant type object instance
Staticobjects
A set of all variables (and their values) stored in the Application object defined by elements
2. Method of Application Object
The method of the Application object allows you to delete values in the global application space and control concurrent access to variables in the space. The methods and descriptions are shown in Table 3-4:
Table 3-4 methods and descriptions of application objects
Method
Description
Contents. Remove ("variable_name ")
Delete a variable named variable_name from the application. Content set.
Contents. removeall ()
Delete all variables from the application. Content set
Lock ()
Lock the application object so that only the current ASP page can access the content. It is used to ensure that concurrent operations by allowing two users to read and modify the value at the same time will not destroy the content.
Unlock ()
Unlocks ASP Web pages on the Application Object
Note: variables cannot be deleted from the application. staticobjects set during running.
3. Application Object events
The Application Object provides two events triggered when it starts and ends, as shown in Table 3-5:
Table 3-5 Application Object events and descriptions
Event
Description
Onstart
Triggered when ASP is started, before the execution of the webpage requested by the user and before any user creates a session object. Used to initialize variables, create objects, or run other code
Onend
Triggered when the ASP application ends. This occurs after the last user session has ended and all the code in the onend event of the session has been executed. When it ends, all variables in the application are canceled
3.3.2 ASP Session Object member Overview
This section describes all the members of a session object.
1. Set of session objects
The Session Object provides two sets for accessing the variables and objects stored in the user's local session space. Table 3-6 shows these sets and descriptions:
Table 3-6 Session object set and description
Integration
Description
Contents
All variables and a set of their values stored in this specific session object, and these variables and values are not defined using elements. Including the variant array and the reference of the variant type object instance
Staticobjects
A set of all variables defined by the element stored in the Session Object
2. Session Object Features
The Session Object provides four attributes. These attributes and descriptions are shown in Table 3-7:
Table 3-7 attributes and descriptions of session objects
Genus
Description
CodePage
Read/write. Integer. Defines the code page used to display page content in a browser ). The code page is the numeric value of the character set. Different Languages and places may use different code pages. For example, ANSI code page 1252 is used in American English and most European languages. Code Page 932 for daily text
Lcid
Read/write. Integer. Defines the page Region ID (lcid) sent to the browser ). Lcid is an abbreviation of an international standard that uniquely identifies a region. For example, 2057 defines that the currency symbol of the current region is 'shanghai '. Lcid can also be used in statements such as formatcurrency, as long as there is an optional lcid parameter. Lcid can also be set in the ASP Processing Command, and takes precedence over the settings in the lcid attribute of the session. This chapter provides a list of ASP processing commands.
Sessionid
Read-only. Long integer. Returns the session identifier of the session. This identifier is generated by the server when the session is created. It is unique only during the lifetime of the parent application object, so it can be reused when a new application starts.
Timeout
Read/write. Integer. Defines the timeout period in minutes for this session. If the user does not refresh or request a webpage during the timeout period, the session ends. You can modify the settings on each webpage as needed. The default value is 10 min. This time should be shorter on sites with high usage
3. Method of Session Object
The Session object allows you to delete a specified value from a user-level session space and terminate the session as needed. The methods and descriptions of the seesion object are shown in Table 3-8:
Table 3-8 methods and descriptions of session objects
Method
Description
Contents. Remove ("variable_name ")
Delete a variable named variable_name from the session. Content set.
Contents. removeall ()
Delete all variables from the session. Content set
Abandon ()
When the web page is executed, end the current user session and undo the current session object. However, even after calling this method, you can still access the variables of the current session on this page. When a user requests the next page, a new session is started and a new session object is created (if any)
Note: variables cannot be deleted from the session. staticobjects set during running.
4. Session Object events
The Session Object provides two events triggered at the start and end, as shown in Table 3-9:
Table 3-9 events and descriptions of session objects
Event
Description
Onstart
Triggered when an ASP user session is started, before the webpage requested by the user is executed. Used to initialize variables, create objects, or run other code.
Onend
Triggered when ASP user session ends. This event is triggered when the user's request to the last page of the application has exceeded the predefined session timeout period. When the session ends, all variables in the session are canceled. This event is also triggered when ASP user sessions are ended using the abandon method in the code.
3.3.3 events using application and session
The application and session objects of ASP reflect the features that other ASP built-in objects do not have-events. However, as shown in the previous object member table, these are all events related to ASP session and application work.
1. Application and session event Processor
ASP triggers an event whenever an application or session starts or ends. You can write common script code in a special file to detect and respond to these events. The file name is global. asa, located in the root directory of an application (the default web site is the inetpubwwwroot directory or a folder defined by the actual application ). This file can contain one or more HTML elements to create component instances that will be used in the application or user session.
In Chapter 4th, We will detail how to create a component instance. The following code is an example of the global. Asa file. We only focus on the elements and the code lines starting with the set Keyword:
Progid = "mswc. counters">
Progid = "mswc. nextlink">
Because this global. the ASA file is used in the example page in this chapter. Therefore, you need to put the file in the root directory of the web site or in the directory configured as a virtual application, the directory contains other sample files.
Read and store values
Note that the preceding example reads the application and session variables in the same way as in the set of request and response objects. Set the values of these variables:
Application ("variable_name") = variable_value
Application ("variable_name") = variant_array_variable_name
Set Application ("variable_name") = object_reference
Obtain 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 method can be used for session objects.
You can see how to "Lock" and "unlock" the application object when accessing from a session event processor; when accessing from an ASP Webpage, the same processing is required. This is not required when you use the code in the Application Event to access the value in the Application object. This is because there is only one application object instance in any application, and the event processor code is only executed when there is no active user session.
You can also see how a basic user session counter is implemented. Here we use an application-level variable visit_count, which is automatically added when a new session starts. Generally, you do not have to simply save the value to the application or session object. For example, a Web Developer's web site has a corresponding global on the http://webdev.wrox.co.uk. asa file. When a new session is started, the file writes corresponding entries to the database on the server. servervariables set. This provides a basic method to count the number of visitors and collect some basic information about the visitors.
2. Create a variant array
The method for creating and using a variant array to store values in the session and application objects is not discussed yet. It is discussed here as a very useful technology. As we can see, a variant data type can contain an array, not just a value.
An array stores a long row of binary values in a specified order in a continuous area of the memory. To arrange variant, You need to point to the first item and provide information about the size and structure. The script engine can do the rest.
You can create a dimension, two-dimensional, or multi-dimensional array in a variant variable, and then assign the array to a variable at the application layer or user session layer, and ensure that the entire array can be used in the corresponding place. The following code demonstrates the use of a simple one-dimensional array:
Dim vararray (3)
Vararray (0) = "This Is"
Vararray (1) = "variant array"
Vararray (2) = "stored in"
Vararray (3) = "Session Object"
Session ("variant_array") = vararray
3. When and when the application and session will start and end
I mentioned this when I introduced how ASP applications and sessions work. The basic terms are as follows:
· When the first user requests an application within the scope (that is, the default root directory of the Web site ), or, when an asp web page of a user-defined virtual application in a sub-directory of the website is started. Occurs before any user session is started.
· When any user initiates a session (if no active session exists) in the default application or an ASP Webpage in a virtual application for the first request ).
· When a user does not download an ASP Webpage within the specified timeout period, the session ends. The timeout value can be used in the script code. the timeout attribute can be set separately for each application in the Properties dialog box, or the default value in the IIS metadatabase can be partially modified through the IIS in Active Directory. After a webpage that calls the session. Abandon method is executed, the session also ends.
· The application ends immediately after the last active session in an application ends.
4. asp processing commands
As shown in chapter 1st, you can add a processing instruction to an asp web page. The processing command can contain more than one entry as needed. The keywords that can be used in the statement and their descriptions are shown in Table 3-10:
Table 3-10 keywords and descriptions of ASP commands
Command keyword
Description
Language = "language_name"
Set the default script language for the web page, for example:
Enablesessionstate = "true" | "fasle"
When it is set to "true", the cookie of a session is prevented from being sent to the browser, so no new session object will be created, and the content of any existing session will no longer be available.
CodePage = "code_page"
Set the webpage code page, as shown in figure
Lcid = "locale_identifier"
Set the location identifier of the webpage, as shown in figure
Transaction = "transaction_type"
Indicates that the webpage file runs in a transaction environment. Valid values:
"Required": if a transaction is available, the script runs in it; if no transaction is available, start a new transaction.
"Requires_new": the script will initialize a new transaction.
"Supported": if a transaction is available, the script runs in it, and a new transaction is not started.
"Not_supported": the script will not run in any existing transactions and will not initialize a new transaction.
Describes transactions in detail in Chapter 1.
Only one processing command can be allowed on a web page, and it should be placed on the first line. The processing command can contain more than one such entry, but must be separated by spaces. There cannot be spaces at both ends of the equal sign. For example:
3.3.4 ASP application object of the activity
We provide some simple web pages that demonstrate ASP application and session objects during use. To work properly, you must put them in a virtual application on the server and put the provided global. Asa file in the root directory of the application. The simplest way is to put the global. Asa file in the root directory of the default web site (C:/inetpub/wwwroot by default.
Renaming any existing global. Asa file is a good way to restore it later.
All the example files in this book can be obtained from our Web site. In the chapter03 sub-directory of the example, there are all other sample webpages in this chapter.
In the chapter03 sub-directory, the default. asp web page is a simple menu that allows you to run the application and session sample web page, as shown in Figure 3-13:
1. display the content of the application set
Click the first link to open the Application Object Example page named show_application.asp. It displays the content of the current application object of the virtual application, as shown in 3-14:
Note that the aspcounter object is a member of the staticobjects set (defined by elements), but the rest (instantiated by server. Createobject) is a member of the contents set.
We can see that the global. Asa Example Web page is placed in these sets, as shown in the previous section:
Progid = "mswc. counters">
...
...
(1) code for traversing the contents set
To traverse the contents set, you can use a for each... Next structure. Each item in the set can be a simple variant type variable, a variant array, or an object reference. Because different processing needs to be performed on each type of value, we have to check each value to identify its type.
You can use the vartype function in VBScript to complete this task. Here, the isobject and isarray functions are used instead:
For each objitem in application. Contents
If isobject (application. Contents (objitem) then
Response. Write "Object Reference: '" & objitem &''
"
Elseif isarray (application. Contents (objitem) then
Response. Write "array: '" & objitem & "' contents are:
"
Vararray = application. Contents (objitem)
'Note: The following only works with a one-dimenwing Array
For intloop = 0 to ubound (vararray)
Response. Write "index (" & intloop & ") =" & _
Vararray (intloop) &"
"
Next
Else
Response. Write "variable: '" & objitem & ''=" _
& Application. Contents (objitem) &"
"
End if
Next
Note how the program retrieves the array from the application object. Assign it to a local variable using the following statement:
Vararray = application. Contents (objitem)
You can use the ubound function to find the array size (number of elements). This value can be used as the termination condition for traversal:
For intloop = 0 ubound (vararray)
This example is a one-dimensional array, and only the content of such an array is displayed. You can edit the code as needed to process multi-dimensional arrays, for example:
For intloop = 0 to ubound (vararray)
Intnumberofdimensions = ubound (vararray, 1)
For intdimension = 0 to intnumberofdimensions
Response. Write "index (" & intloop & ") =" _
& Vararray (intloop, intdimension)
Next
Response. Write"
"
Next
(2) code for traversing the staticobjects set
The staticobjects set contains all object references that use element declarations in global. Asa. Because each entry is an object variable, you can use simple code to traverse the array. We will output the Object Name (the original definition in the ID attribute ):
For each objitem in application. staticobjects
If isobject (application. staticobjects (objitem) then
Response. Write "element: Id = '" & objitem &''
"
End if
Next
(1) code for traversing the contents set
To traverse the contents set, you can use a for each... Next structure. Each item in the set can be a simple variant type variable, a variant array, or an object reference. Because different processing needs to be performed on each type of value, we have to check each value to identify its type.
You can use the vartype function in VBScript to complete this task. Here, the isobject and isarray functions are used instead:
For each objitem in application. Contents
If isobject (application. Contents (objitem) then
Response. Write "Object Reference: '" & objitem &''
"
Elseif isarray (application. Contents (objitem) then
Response. Write "array: '" & objitem & "' contents are:
"
Vararray = application. Contents (objitem)
'Note: The following only works with a one-dimenwing Array
For intloop = 0 to ubound (vararray)
Response. Write "index (" & intloop & ") =" & _
Vararray (intloop) &"
"
Next
Else
Response. Write "variable: '" & objitem & ''=" _
& Application. Contents (objitem) &"
"
End if
Next
Note how the program retrieves the array from the application object. Assign it to a local variable using the following statement:
Vararray = application. Contents (objitem)
You can use the ubound function to find the array size (number of elements). This value can be used as the termination condition for traversal:
For intloop = 0 ubound (vararray)
This example is a one-dimensional array, and only the content of such an array is displayed. You can edit the code as needed to process multi-dimensional arrays, for example:
For intloop = 0 to ubound (vararray)
Intnumberofdimensions = ubound (vararray, 1)
For intdimension = 0 to intnumberofdimensions
Response. Write "index (" & intloop & ") =" _
& Vararray (intloop, intdimension)
Next
Response. Write"
"
Next
(2) code for traversing the staticobjects set
The staticobjects set contains all object references that use element declarations in global. Asa. Because each entry is an object variable, you can use simple code to traverse the array. We will output the Object Name (the original definition in the ID attribute ):
For each objitem in application. staticobjects
If isobject (application. staticobjects (objitem) then
Response. Write "element: Id = '" & objitem &''
"
End if
Next
1. add value to the contents set
The method for adding value to the contents set is the same as that used in the script code on the global. Asa web page. The sample Web page allows you to add a new variant value to the Application Object and has a suggested name and value (which can be edited as needed), as shown in 3-15:
Click the button to reload the webpage, add the value to the application. Contents collection, and display it in the list, as shown in 3-16:
Add a new contents entry code
All buttons and other HTML controls are placed on a form in the sample webpage. Action sets the path of the current webpage. When submitting the form, reload it. The method property is "Post", so the value in the control appears in the request. Form set. These two technologies have been used in previous chapters:
"Method =" Post ">
Your session has been terminated
A newSessionWill be started when you load another
ASP page. It will contain any values that are defined in
TheGlobal. asaFile for this application.
Return to the previous page
Result 3-21 is displayed. At this time, the current user session has been terminated (abandoned), and the user cannot reference the content of the original session set or attribute:
However, remember that the user's session content can still be obtained from the session object even after the call to the abandon method is completed. When the web page ends, the session ends.
Of course, a new ASP session is started when the sample page of the session object displaying the session content is returned. It has a different start_time value and other default session values created by executing the code in global. Asa, as shown in 3-22:
Note that the attribute value of session. sessionid has not changed. ASP tries to re-allocate the same session ID, so it cannot rely on this value to determine that a new session has been started.
Conclusion 3.4
This chapter introduces two asp built-in objects: Application and session objects. These objects introduce the concepts of ASP applications and user sessions (both are specific terms and are not the meaning of common conversations ). ASP applications allow you to assign special properties to page sets, defining how IIS and ASP manage these webpages and other components used.
However, the main reason for using ASP applications and sessions is to automatically obtain the status. In other words, the ability to store information and variable references is either global and available to all webpages loaded to the user (that is, in an application ), it is either only available for all webpages of a specified user (in a session ). This makes creating a web application very simple (that is, the application works on the web, but can complete the specified task just like a traditional compiled application ).
This chapter describes how to use ASP application and session objects on some sample pages. These pages demonstrate all the technologies available for these two objects.