application| Objects | Built-in Objects | detailed
In addition to the objects used to send, receive, and process data in an ASP's built-in objects, there are some very useful objects that represent Active Server applications and individual user information.
Let's take a look at the Application object first. All. asp files in the same virtual directory and its subdirectories make up the ASP application. Instead of using the Application object, we can share information between all users of a given application and persist the data during the server's run. Also, the Application object has control over how the application-tier data is accessed and the events that can be used to trigger the process when the application starts and stops.
Now let's learn application objects together.
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. Because the values stored in the Application object can be read by all users of the application, 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.
Let's take a look at the following program that uses application to record the number of page visits:
<%
Dim numvisitsnumvisits=0
Application.lockapplication ("numvisits") = Application ("NumVisits") + 1
Application.UnLock
%>
Welcome to this page, you are the first <%= application ("NumVisits")%> visitors!
By saving the above script in your. asp file, you can easily add a counter to your page.
2, in contrast to the Lock method, the Unlock method allows other customers to modify the properties of the Application object.
In the above example, the Unlock method unlocks the object so that the next client can increase the value of the numvisits.
III. Events
1, Application_OnStart
The Application_OnStart event occurs 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. For example, each of the following lines returns an error.
<%
Set application ("Var1") =session
Set application ("Var2") =request
Set application ("Var3") =response
Set application ("VAR4") =server
Set application ("VAR5") =application
Set application ("VAR6") =objectcontext
%>
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"%>