9. Ninth ASP Built-in Object Application and Session

xiaoxiao2021-03-06  41

In the previous author, I will introduce you to the use of ASP built-in object response, in which the author will continue to introduce you to the other two very practical and important ASP built-in object Application and Session. In addition to the objects used to send, receive, and process data in the built-in object of the ASP, there are also some objects that represent the Active Server application and a single user information.

Let's take a look at the Application object. All .asp files in the same virtual directory and its subdirectory constitute an ASP application. We don't use the Application object, share information between all users of a given application, and save data during the server run. Moreover, the Application object also has an event that controls access to application layer data and an event that can be used to trigger the process when the application starts and stops.

Let us learn the Application object together.

First, attribute

Although the Application object does not have built-in attributes, we can use the following syntax to set the user-defined attributes.

Application ("Properties / Collection Name") = value

We can use the following scripts to declare and create the properties of the Application object.

<%

Application ("MyVar") = "Hello"

Set Application ("MyObj") = Server.createObject ("MyComponent")

%>

Once we assign the properties of the Application object, it will last forever until the Web Server service is turned off to stop. Since the value stored in the Application object can be read by all users of the application, the properties of the Application object are particularly suitable for transmitting information between applications.

Second, the method

The Application object has two methods that are all useful to handle multiple users to write data stored in Application.

1, the LOCK method prohibits other customers to modify 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 Application variables at the same time. If the user does not explicitly call the UNLock method, the server will be locked to the Application object after the end or timeout of the .asp file.

Let's take a look at the program that uses Application to record the number of page access:

<%

DIM NumvisitsNumVisits = 0

Application.lockApplication ("Numvisits") = Application ("Numvisits") 1

Application.unlock

%>

Welcome to this page, you are the "Numvisits")%> A visitor!

Save the above scripts in your .asp file, easily add a counter to your page.

2, and the LOCK method, the UNLOCK method allows other customers to modify the properties of the Application object.

In the above example, in the above example, the UNLOCK method relieves the lock of the object such that the next client can increase the value of NumVisits.

Third, an event

1, Application_OnStart

The Application_onstart event occurs before the first creation of a new session (ie, the session_onstart event). The Application_onstart event is triggered when the web server starts and allows the files included in the application. The process of Application_onstart event must be written in the global.asa file. The syntax of the Application_onstart event is as follows: