Uncovering the Mystery of ASP (4)

Source: Internet
Author: User
Application objects

Author: Sage of the Rice Fairy

A collection of application objects
There are two sets of contents and Staticobject in application, and you can use these two sets to implement controls that are visible to variables and objects throughout the ASP application.
The Contents collection is a set of variables that are visible to each session in an entire ASP application. These variables are used at the application level, and you can use Application.Contents ("currentdiscount") = 1, because this collection is the default collection of application objects, so The preceding line of code can also be written in the following way: Application ("Currentdiscount") = 1.

Staticobject Collection This collection is created from the Global.asa file, the Global.asa file is part of an ASP application that provides a way for you to write code to run when an ASP application starts or terminates, and it provides the ability to write code to run at the beginning and end of the session.
One, property
Although the Application object has no built-in properties, we can use the following syntax to set a user-defined property, also known as a collection.
Application ("Attribute/collection name") = value
We can declare and establish the properties of the Application object using the following script.
<%
Application ("MyVar") = "Hello"
Set application ("myobj") = Server.CreateObject ("MyComponent")
%>
Once we assign the properties of the Application object, it persists until the WEB Server service is turned off so that application stops. The values stored in the Application object can be read by all users of the application, so the properties of the Application object are particularly suitable for passing information between users of the application.

Second, the method
The Application object has two methods, all of which are used to work with multiple users writing to the data stored in application
1. The Lock method prevents other customers from modifying the properties of the Application object.
The Lock method prevents other customers from modifying variables stored in the Application object to ensure that only one customer can modify and access the application variable at the same time. If the user does not explicitly call the Unlock method, the server will unlock the Application object after the. asp file finishes or times out.
2, in contrast to the Lock method, the Unlock method allows other customers to modify the properties of the Application object. The Unlock method unlocks the object so that the next client can increase the value of the numvisits.

III. Events
1. The Application_OnStart Application_OnStart event is sent before the first time a new session (that is, a Session_OnStart event) is created. The Application_OnStart event is triggered when the WEB server starts and allows requests for files that are contained by the application. The process of Application_OnStart events must be written in the Global.asa file.
The syntax for the Application_OnStart event is as follows:
< SCRIPT language=scriptlanguage runat=server>
Sub Application_OnStart
. . .
End Sub
</script>

2, Application_OnEnd
The Application_OnEnd event occurs after the application exits after the Session_OnEnd event, and the process of Application_OnEnd events must also be written in the Global.asa file.

Let's take a look at some of the things you need to be aware of when using application objects.
You cannot store an ASP-built object in a Application object. If you store an array in a Application object, do not directly change the elements stored in the array. For example, the following script cannot be run.
<% Application ("StoredArray") (3) = "New Value"%>
This is because the Application object is implemented as a collection. The array element StoredArray (3) did not get a new assignment. This value will be included in the Application object collection and will overwrite any information previously stored in this location. It is recommended that when you store an array in a Application object, you get a copy of the array before you change the object in the array. In an array operation, you should store all of the arrays in the Application object, so that any changes you make will be stored.



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.