In a Web application, when a user accesses the application, the Session type variable allows the user to share data on all pages of the Web application; if another user accesses the Web application at the same time, the user also has his own Session variables, but the two users cannot share information through the Session variables, application-type variables allow multiple users on the site
PageShare information. 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 ASP applications. Instead of using the Application object, we can share information among all users of a given Application and persistently save data during server running. In addition, the Application object also provides methods to control access to Application layer data and events that can be used to trigger the process when the Application starts or stops.
1. Application ("name") = Value
Just like Session ("name") = value, the Application object does not have any built-in attributes. Of course, you can customize an attribute, which is also called a set.
Once an Application object is assigned a property, it will persist until the WEB Server service is disabled to stop the Application. Because the values stored in the Application object can be read by all users of the Application, the attributes of the Application object are especially suitable for transmitting information between users of the Application.
<% Application ("MyName") = "cnbruce" %> |
II. Application. Lock
The Lock method prohibits other users from modifying the properties of the Application object to ensure that only one customer can modify and access the Application variable at the same time. If you do not explicitly call the Unlock method, the server will Unlock the Application object after the. asp file ends or times out. The simplest example is the page count.
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 calculate the initial value, 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 refresh will accumulate the number of records. For example, if the number is accessed by IP value, a Session will be created.
2. vnum. asp
<% If session ("visitnum") = "" then Application. Lock Application ("NumVisits") = Application ("NumVisits") + 1 Application. Unlock Session ("visitnum") = "visited" End if %> You are the <% = Application ("NumVisits") %> visitor on this page |
3. Application. Unlock
In contrast to the Lock method, the Unlock method allows other users to modify the attributes of the Application object. In the preceding example, the Unlock method unlocks the object so that the next client can increase the value of NumVisits.
Of course, it should be noted that the count should ensure that the server is not restarted, because the access value is based on the page, and is not saved as a file or saved to the database.
Generally, Application events are triggered when the server is restarted.
Iv. Application_OnEnd
The Application_OnEnd event occurs after the Session_OnEnd event when the application exits. Of course, the process of processing the Application_OnEnd event must also be written in the Global. asa file.
For example, in the above program, if the server is shut down, it will inevitably trigger the Application_OnEnd event, so that the event can be saved, and the next data continuation is enabled.
Of course, the Application_OnEnd event occurs after the Session_OnEnd event when the application exits.
<Script language = "VBScript" RUNAT = "Server"> ............. Sub Application_OnEnd ............. End Sub ............. </SCRIPT> |
V. Application_OnStart
The Application_OnStart event occurs before the first session (Session_OnStart event) is created. The Application_OnStart event is triggered when the WEB server starts and allows requests to files contained in the application.
<Script language = ScriptLanguage RUNAT = Server> ............. Sub Application_OnStart ............. End Sub ............. </SCRIPT> |
So now we can imagine the code inside Global. asa.
<Script language = "VBScript" RUNAT = "Server"> Sub Application_OnStart ..................... End SubSub Session_OnStart ..................... End Sub Sub Session_OnEnd ..................... End Sub Sub Application_OnEnd ..................... End Sub </SCRIPT> |
What is Global. asa? Finally, let's break it down again :)
The five objects are basically mastered. Let's take an example and practice it.
<% @ LANGUAGE = VBScript %> <% Option Explicit %> <Html> <Title> guess digital games </title> <Body> <% 'Set the page to not use the cache Response. Expires = 0 %> <% Dim GuessNum On error resume next GuessNum = Request ("Number ") If GuessNum = "" then GuessNum = "0" End if GuessNum = Clng (GuessNum)Session ("Count") = Session ("Count") + 1 If Session ("Count") <10 and GuessNum <> session ("Number") then %> <Form action = "guessNumber. asp"> <Input type = "text" name = "Number"> <Input type = "submit" value = "submit"> </Form> <Hr> <% End if If GuessNum <0 or guessNum> 100 then Response. write "Enter 1 ~ Integer between 100" Elseif GuessNum = 0 then Session ("Count") = 0 Randomize Session ("Number") = Int (rnd * 100 + 1) Response. write "Enter 1 ~ Integer between 100" Elseif GuessNum> session ("Number") then Response. write "You guessed too much" Elseif GuessNum <session ("Number") then Response. write "You guessed too small" Elseif GuessNum = session ("Number") then Response. write "Congratulations, you guessed it" End if Response. write "<br> guessed" & Session ("Count") & "times" If Session ("Count") = 10 then Response. write "the answer is" & Session ("Number ") End if %> <A href = "guessnumber. asp? Number = 0 "> re-guess </a> </Body> </Html> |
Test URL: http://www.cnbruce.com/test/aspcode/guessnumber.asp