ASP's application and Session object detailed explanation (two)

Source: Internet
Author: User
Tags variables servervariables sessions root directory
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.