ASP Lecture 3: Application Object and Session Object
Author:Eve Cole
Update Time:2009-05-30 19:58:17
In the previous lecture, we learned about the Form data collection, QueryString data collection and ServerVariables data collection of the Request object. Before continuing with the following study, it is recommended that you relax first, because the Application object to be introduced next is relatively abstract, and you may not be able to distinguish things when you first understand it. Remember: when you can't find it, don't think too much about what the Application object is? As the old saying goes, learn to use it first.
1. Understand the Application object. To refresh your mind, let's look at a counter routine (you should run it to understand it):
First edit a wuf16.htm file:
<html>
<body>
Example of recording the number of clicks on a page: <a href="wuf17.asp">wuf17.asp</a>
</body>
</html>
The code of wuf17.asp is as follows:
<% @LANGUAGE = VBScript %>
<% 'wuf17.asp - Record the number of visits to a certain page (counter principle)
Option Explicit
Dim CountWeb
CountWeb = Request.ServerVariables("SCRIPT_NAME")
'This ensures that each page has a unique variable to avoid confusion
%>
<HTML><BODY>
<%
Response.Write CountWeb & "<Br><Br>" & vbcrlf
Application.Lock 'Please read the explanation
Application(CountWeb) = Application(CountWeb) + 1 'Accumulator, add 1 to the number of clicks
Application.UnLock
Response.Write "The total number of clicks on this page before the WEB server is shut down and restarted is: " & Application(CountWeb)
%>
</BODY></HTML>
When running this program, for most people, there is only one machine, which is both the Web server and the client. Then you can only imagine a situation like this: your two homepage files are placed on a far away Web server, and a flood of domestic and foreign users are accessing them through browsers. Once each user clicks, the value of Application(CountWeb) will be incremented by 1, and as long as the server storing the file wuf17.asp is not closed, Application(CountWeb) will continue to accumulate. But note that if the server is shut down and then restarted, Application (CountWeb) will start counting from 0 again (I'm confused, I said why the number of my page visits is always one digit). In order to avoid this kind of counting situation that always starts from zero, you must save the value of Application (CountWeb) before the server is shut down, and take it out when needed next time, so that it can be accumulated sequentially. How to do it, please listen Decompose after N times.
The next question is what does Application.Lock and UnLock mean? We just made an assumption that a large number of users are visiting the page, and each user's click will cause the Application (CountWeb) to increase by 1. Think about it, wouldn't this be a mess? (What! I can't think of it, it must be you (Only you are the only one who sponsors the homepage), so there must be a first-come, first-served rule. When a user visits the page and needs to modify the value of Application (CountWeb), use the Lock method to lock it to prevent other users from modifying it. After the modification is completed, unlock it.
Have you seen from the above example that the Application object provides shared information (Application(CountWeb)) to all users, and it is oriented to all users (each user's access will cause the counter to increase by 1). The Application object also has two events, Application_OnStart event and Application_OnEnd event (don’t be confused, this is just a starting concept in object-oriented programming languages). The syntax when using it is as follows:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SubApplication_OnEnd
'When the WEB server is closed, the Application_OnEnd event will be activated
'That is, the program will execute the code placed here
'So before the server is shut down, we can change the value of Application (CountWeb) in wuf17.asp here
'Save it, such as in a text file.
End Sub
Sub Application_OnStart
'When the first user browses the ASP web page, the Application_OnStart event will be activated, but when other users browse in the future, this event will not occur.
'That is, the program will execute the code placed here
'So the value of Application(CountWeb) saved in the text file can be read here.
End Sub
</SCRIPT>
The following is an example to illustrate how to use events. The function of this program is to count the total number of clicks on all pages of the site.
The code of the file wuf18.asp (other page files in the site are similar to this file):
<% @LANGUAGE = VBScript %>
<% Option Explicit
'All page files in the site should include this sentence <!--#include file="wuf19.asp"-->%>
<!--#include file="wuf19.asp"-->
<HTML><BODY>
The total number of hits on all pages of the site before the server is shut down and restarted: <%= Application("CountAll") %>
</BODY></HTML>
Code of file wuf19.asp:
<% ' wuf19.asp
Application.Lock
Application("CountAll") = Application("CountAll") + 1
Application.UnLock
%>
The files wuf18.asp and wuf19.asp are placed in the same directory. Code of file global.asa:
Note: The event handler must be saved as a text file named "global.asa" and must be placed in the root directory of the virtual path (usually in the same directory as the index.htm or default.htm file, for example: Take my machine as an example. In terms of absolute paths, global.asa is placed under c:InetPubhome, while wuf18.asp and wuf19.asp are placed under c:InetPubhomeasp). The same virtual path only A global.asa file is allowed to exist.
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
'global.asa - wuf18.asp The file name in the download package is 18global.asa, which needs to be renamed when using it.
Sub Application_OnStart
'When the first user browses the web page, set the initial value of the counter to 0.
'When other users browse in the future, the following code will no longer be executed.
Application.Lock
Application("CountAll") = 0
Application.UnLock
End Sub
</SCRIPT>
Browse wuf18.asp to view the running results. It seems that it is not difficult to just learn to use the Application object.
2. Understand the Session object. With the previous Application object as a foreshadowing, the Session object is much easier. It is similar to the Application object, except that it is only used to record information related to a single user. It is oriented to a single user, so we can use the Session object to store information about a single user.
Like the Application object, the Session object also has two events: Session_OnStart event and Session_OnEnd event. The usage syntax is similar to the Application object.
It is worth mentioning that before using the Session object, you must confirm that the browser's Cookie function is enabled (the default setting is fine).
Here is an example. The requirements for the file global.asa are the same as above, so the global.asa in the above example will be overwritten by the global.asa in this example.
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
' global.asa - wuf20.asp The file name in the download package is 20global.asa
SubSession_OnStart
Session("In") = Now 'Function Now gets the current time
End Sub
SubSession_OnEnd
Session("Out") = Now
Application.Lock
Application("Out") = Session("Out")
Application("In") = Session("In")
Application(“Out”).UnLock
End Sub
</SCRIPT>
File wuf20.asp:
<%@ Language=VBScript %>
<HTML>
<BODY>
<% 'wuf20.asp
Response.Write "Default Timeout settings:" & Session.Timeout & "Minutes<Br>"
Session.Timeout = 1
Response.Write "The time you entered this site:" & Session("In") & "<Br>"
Response.Write "Please refresh this page in 1 minute" & "<Br><Br>"
if Not IsEmpty(Application("Out")) then
Response.Write "The last time you entered this site:" & Application("In") & "<Br>"
Response.Write "The last time you left this site:" & Application("Out") & "<Br>"
Response.Write "Browsing time (minutes):" &_
Datediff("n",Application("In"),Application("Out")) & "<Br><Br>"
End If
if IsEmpty(Session("Out")) then
Response.Write "The value of Session(""Out"") before refreshing and after refreshing is empty" & "<Br>"
End If
%>
</BODY>
</HTML>
When the user browses the web page for the first time, the Session_OnStart event occurs. The program records the time when the user enters the web page. However, the Session_OnEnd event does not occur at this time, so the Application ("Out") value is empty and the program does not display the browsing time.
We know that the lifetime of the Application object is from the first user browsing the web page until the server is shut down. The lifetime of the Session object is from the first time the user browses the web page (the beginning of the session) until the end of the session. So when does the session end? Specifically in this example, Session.Timeout = 1 sets the session timeout to 1 minute, that is, if no request is provided to the web server for 1 minute, the session times out, causing the session to end, the Session_OnEnd event occurs, and the program executes the code in Session_OnEnd. Here we put the entry and exit times into Application("In") and Application("Out") respectively, because after the session ends, the Session object will no longer exist. We verified this at the end of the code.
From this routine, we can see:
1. Application objects and Session objects have different lifetimes.
2. The Session object is for a single user, and its value is stored on the client. Browsing by other users has no impact on the single-user Session object. A single user can only read and write the value of his own Session object (for example: Session("In")).
3. Note the representation of double quotes in Response.Write: (""Out"").
4. Special reminder: This program has no practicality and is only for debugging (only suitable for browsing by only one user). Please think about why this is? The answer is at the end of this article.
Tip: Please pay attention to the characteristics of the Application object.
3. A classic example: How to display the number of people online on a web page. In the following example, Application ("Online") is used to store the number of people online, and Application ("CountAll") is used to store the total number of visitors.
Principle: When a new user browses the web page, the Session_OnStart event occurs, the number of online users increases by 1, and the number of visiting users also increases by 1. When a user leaves, the Session_OnEnd event occurs after the session times out, and the number of people online is reduced by 1.
The Global.asa code is as follows:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
'Global.asa-wuf21.asp
Sub Application_OnStart
Application.Lock
'Read the initial value of Application("CountAll") here
Application.UnLock
End Sub
SubSession_OnStart
Application.Lock
Application("Online") = Application("Online") + 1
Application("CountAll") = Application("CountAll") + 1
Application.UnLock
End Sub
SubSession_OnEnd
Application.Lock
Application("Online") = Application("Online") - 1
Application.UnLock
End Sub
SubApplication_OnEnd
Application.Lock
'Application("CountAll") should be saved to the file here
Application.UnLock
End Sub
</SCRIPT>
Wuf21.asp code is as follows:
<%@ Language=VBScript %>
<HTML>
<BODY>
<%Session.Timeout = 5%>
<P>Number of people online: <%= Application("Online")%></P>
<P>Total number of visitors: <%= Application("CountAll")%></P>
</BODY>
</HTML>
Please note that this routine is compared with wuf17.asp. The latter counts the number of clicks, while the former counts the number of visitors.
Answer: Since the Application object applies to all users, in the case of multi-users, other users' browsing will also change the values of Application ("In") and Application ("Out").