Parsing ASP's application and session objects

Source: Internet
Author: User
Tags date array arrays character set include numeric value variables variable
Application|session| object in a published series of articles, we have discussed two ASP objects: application objects and Session objects, so we have access to the collections, methods, and application provided by the objects and the session object. Properties and events. This section will study these two objects from the perspective of programming.

· When loading an ASP DLL and responding to the first request for an ASP Web page, create the Application object. This object provides a storage place to store variables and objects that are available to all Web pages that are open to all visitors.
  
· When the visitor first requests an ASP page from the site, create a session object for him and remain valid until the default time-out period (or the timeout period determined by the script). This object provides a storage place to store variables and objects that are available only to pages that the visitor opens during the session's activity.

   Relationship between

1, ASP's Application Object member overview

This section describes the collection, methods, and events of the Application object (Application object has no attributes). The Session object (with attributes) is described in the next section. You will then continue to explore the tasks accomplished by using these objects and describe 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:

The collection and description of table application objects

Collection Description
Contents A collection of all the variables (and their values) stored in the Application object that are defined by the element is not used. References that include variant arrays and instances of variant type objects
StaticObjects A collection of all the variables (and their values) that are stored in the Application object, as defined by the element

2. Methods of Application objects

The Application object method allows you to delete values in the global application space and control concurrent access to variables within that space. Method and description as shown in the following table:

Method and description of table Application Object

Method Description
Contents.Remove ("Variable_name") To remove a variable named Variable_name from the Application.content collection
Contents.RemoveAll () Remove all variables from the Application.content collection
Lock () Locks the Application object so that only the current ASP page can access the content. A concurrent operation that ensures that two users are allowed to read and modify this value at the same time does not break the content
Unlock () Unlock an ASP Web page on a Application object

Note that you cannot delete a variable from the Application.staticobjects collection during run time.

3. Events for 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 for table application objects

Event Description
OnStart Triggered when ASP starts, before the Web page that the user requests is executed and before any user creates the Session object. For initializing variables, creating objects, or running other code
OnEnd Triggered when an ASP application is finished. 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 present in the application are canceled

   ASP's Session Object member overview

This section outlines all members of the session object.

1. A collection of Session objects

The Session object provides two collections that can be used to access variables and objects stored in the user's local conversation space. These collections and descriptions are shown in the following table:

A collection of table session objects and descriptions

Collection Description
Contents A collection of all variables and their values stored in this particular session object, and these variables and values are not defined using the element. References that include variant arrays and instances of variant type objects
StaticObjects A collection of all the variables that are stored in this session object, defined by using the element

2. Attributes of the Session object

The Session object provides four properties. These attributes and descriptions are shown in the following table:

Table 3-7 Properties and descriptions of Session objects

Property Description
CodePage Read/write. Integral type. Defines the code page that is used to display the content of a page in a browser. The code page is the numeric value of the character set, and 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 Japanese text
Lcid Read/write. Integral type. Defines the page region identity (LCID) that is sent to the browser. The LCID is an international standard abbreviation that uniquely identifies a region, for example, 2057 defines the currency symbol for the current region as ' £ '. The LCID can also be used in statements such as FormatCurrency, as long as there is an optional LCID parameter. The LCID can also be set in the ASP processing instruction <%...%> and takes precedence over the settings in the LCID attribute of the session. A list of ASP processing instructions is provided later in this chapter
SessionID Read-only. Long integral type. Returns the session identifier for this session, which is generated by the server when the session is created. Only within the lifetime of the parent Application object, so a new application can be reused when it is started
Timeout Read/write. Integral type. Defines a time-out period in minutes for this session. If the user does not refresh or request a Web page during the timeout period, the session ends. Can be modified as needed on each Web page. The default value is 10min. The time should be shorter on a site with a high usage rate

3. Method of Session Object

The Session object allows you to delete the specified value from the user-level session space and terminate the conversation as needed. The method and description of the Seesion object are shown in the following table:

Method and Description of table session object

Method Description
Contents.Remove ("Variable_name") To 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 completes, the current user session is terminated and the current sessions object is undone. However, even after calling the method, you can still access the variables of the current session in the page. When the user requests the next page, a new session is started and a new sessions object is created (if present)
Note that you cannot delete a variable from the Session.StaticObjects collection during run time.

4. Event for Session Object

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 Description
OnStart Triggered when an ASP user session is started, before the Web page that the user requests is executed. Used to initialize variables, create objects, or run other code.
OnEnd Triggered when an ASP user session ends. Starts from the user's last page request to the application, triggering the event if it has exceeded the scheduled session time-out period. When the session ends, cancels all variables in the session. This event is also triggered when an ASP user session is terminated using the Abandon method in code
   events using application and session

ASP's application and session objects embody features that are not in the other ASP's built-in objects-events. However, as you can see in the previous object member tables, these are events associated with the work of ASP sessions and applications.

1. Event handlers for application and sessions
 
An ASP triggers an event whenever an application or session is started or terminated. These events can be detected and answered by writing ordinary scripting code in a special file named Global.asa, located in the root directory of an application (for the default Web site is the \inetpub\wwwroot directory, Or as a folder that is defined as a real application program. This file can contain one or more HTML <OBJECT> elements to create a component instance that will be used within that application or user session.

The following code is an example of a Global.asa file. We only focus on the <OBJECT> elements and the 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 a 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"
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"
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" session. This is the *only*
' Place ', the ASP page, is available like this.
' As a 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 for the sample pages in this chapter, you will need to place the file in the root directory of the Web site, or in a directory that is already configured as a virtual application, and include other sample files in that directory.

   reading and storing values

Note how the above example reads application and session variables in the same way that they are taken in the collection 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
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, you can take the same approach for session objects.

You can see how to lock and unlock (unlock) The Application object when accessing from a session event handler, and you need to do the same when accessing from an ASP Web page. This is not required when you access the values in the Application object with code in the application event. This is because there is only one instance of a Application object in any application, and the code for its event handler only occurs when there is no active user session.

You can also see how a basic User session counter is implemented. This uses an application-level variable visit_count, which automatically increases when a new session is started. It is generally not limited to simply saving values to application or session objects. For example, a web Developer's Web site is http:// webdev.wrox.co.uk, there is a corresponding Global.asa file, when a new session is started, the file writes the corresponding entry in the database on the server, and the data details are obtained from the Request.ServerVariables collection. This provides a basic method of counting the number of visitors and collecting some basic information about the visitors.

Related Article

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.