Application Object and Session Object

Source: Internet
Author: User
Tags servervariables
3.3 ASP application object and Session Object
This chapter has discussed two ASP objects: Application Object and Session object. Therefore, you can access the set, method, attribute, and event provided by the Application Object and Session object. This section will Program From the design perspective, the two objects are studied.
? 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.

Figure 3-12 ASP object relationship

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 the <Object> element. 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 the <Object> element

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 otherCode 

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 the <Object> element. Including the variant array and the reference of the variant type object instance

Staticobjects
A set of all variables defined by the <Object> element stored in this 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 the currency symbol of the current region as '£ '. Lcid can also be used in statements such as formatcurrency, as long as there is an optional lcid parameter. Lcid can also process commands in ASP <%... %>, 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 use application and session events
Asp's application and session objects reflect the features that other ASP built-in objects do not have ?? Event. 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 this application or user sessions.
Chapter 4th describes how to create a component instance. The following code is an example of the global. Asa file. We only focus on the element and the code lines starting with the set Keyword:
with application-level scope // -->
progid = "mswc. counters ">

<! -- 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"
Vararray (1) = "variant array"
Vararray (2) = "stored in"
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"
Vararray (1) = "variant array"
Vararray (2) = "stored in"
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. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.