In a web application, when a user accesses the application, a variable of the Session type can allow the user to share data in all pages of the Web application; if another user also accesses the Web application at the same time, he also has his own Session variable, but two users cannot share information through the Session variable, while a variable of the Application type can enable multiple users of the site to share information in all pages. It can be understood that Session is a local variable, while Application is a global variable.
All .asp files in the same virtual directory and its subdirectories constitute the ASP application. Instead of using Application objects, we can share information among all users of a given application and save data persistently during server operation. Furthermore, the Application object also has methods to control access to application-level data and events that can be used to trigger a process when an application starts and stops.
1. Application("name")=Value
Just like Session("name")=value, the Application object does not have built-in properties. Of course, users can customize attributes, which can also be called collections.
Once the properties of the Application object are assigned, it persists until the WEB server service is shut down so that Application stops. Since 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.
<% Application("MyName") = "cnbruce" %> |
2. Application.Lock
The Lock method prohibits other users from modifying the properties of the Application object to ensure that at the same time there is only one customer who can modify and access the Application variable. If the user does not explicitly call the Unlock method, the server will unlock the Application object after the .asp file ends or times out. The easiest example is to do page counting.
1, num.asp
<% Application.Lock Application("NumVisits") = Application("NumVisits") + 1 Application.Unlock %> You are the <%=Application("NumVisits")%> visitor on this page |
Of course, if you need to remember the initial value of the number, you should write a judgment.
<% if Application("NumVisits")<9999 then Application("NumVisits")=10000 end if Application.Lock Application("NumVisits") = Application("NumVisits") + 1 Application.Unlock %> You are the <%=Application("NumVisits")%> visitor on this page |
In the above program, you will find that every time you refresh, the count will be accumulated. If you access the IP value to count, a session will be created.
2, vnum.asp