Application object and Session object

Source: Internet
Author: User
Tags current time include modify servervariables client root directory
In the last lecture, we learned the form data collection of the Request object, the QueryString data collection, and the ServerVariables data collection. Before continuing the following study, I suggest you to relax, because the next to introduce the Application object relatively abstract, just beginning to understand I am afraid will not distinguish things. Remember: When you can't find the north, don't think too much about what the Application object is. Or an old saying, learn to use it first.

First, understand the Application object. To refresh, let's look at the routine of a counter (you should run it to make it easier to understand):
First edit a wuf16.htm file:
<body>
Record the number of clicks on a page sample: <a href= "wuf17.asp" >wuf17.asp</a>
</body>
The code for wuf17.asp is as follows:
<% @ LANGUAGE = VBScript%>
<% ' wuf17.asp-record the number of accesses to a 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, click Number plus 1
Application.UnLock
Response.Write "The total number of clicks on the page before the Web server shuts down is:" & Application (Countweb)
%>
</BODY></HTML>
When running this program, for most people, there is only one machine, both a Web server and a client. Then you can only imagine the situation: your two home pages are on a very remote Web server, and as the tide of domestic and foreign users are accessing them through the browser. Each user clicks on the value of application (Countweb) plus 1, and as long as the file wuf17.asp server is not closed, application (Countweb) will continue to accumulate. Note, however, that if the server is powered down and restarted, application (Countweb) will start counting again from 0 (I'm dizzy, I say my page access number is always a number). To avoid this forever zero-based count, you have to save the value of the application (Countweb) before the server shuts down, and then take it out again the next time you need it, to be able to accumulate the order, how to do it, and listen to the N-back decomposition.
The next question is, what does application.lock and unlock mean? Just now we made a hypothesis, a large number of users are accessing the page, and each user's click will occur application (Countweb) plus 1, think about it, so don't mess up (what!) Can't think of, that must be your homepage all day long only you one person sponsor, therefore must have a arrival rule. When a user accesses the page, you need to modify the value of application (Countweb), use the Lock method to lock, to avoid other users to modify, after the modification, then unlock.
As you can see from the example above, the Application object provides all users with shared information (Application (Countweb)), which is intended for all users (each user's access will result in a counter plus 1). The Application object also has two events, Application_OnStart events and Application_OnEnd events (you should not faint, this is only the beginning of the Object-oriented programming language), using the following syntax:
<script Language=vbscript runat=server>
Sub Application_OnEnd
' When the Web server shuts down, the Application_OnEnd event is activated
' That's where the program will execute the code here.
' So before the server shuts down we can place the value of application (Countweb) in wuf17.asp
' Save, as in a text file.
End Sub

Sub Application_OnStart
' When the first user browses the ASP page, the Application_OnStart event is activated, and the event does not occur when other users browse later.
' That's where the program will execute the code here.
' So here you can read the value of application (Countweb) saved in a text file.
End Sub
</SCRIPT>
Here is an example of how to use an event that counts the total number of clicks on all pages in the site.
File wuf18.asp code (similar to this file for other page files in the site):
<% @ LANGUAGE = VBScript%>
<% Option Explicit
All page files in the station should include this sentence <!--#include file= "wuf19.asp"-->%>
<!--#include file= "wuf19.asp"-->
<HTML><BODY>
Total number of page hits on site before server shutdown reboot: <%= application ("Countall")%>
</BODY></HTML>
Code for File wuf19.asp:
<% ' wuf19.asp
Application.Lock
Application ("Countall") = Application ("Countall") + 1
Application.UnLock
%>
The file wuf18.asp is placed in the same directory as the wuf19.asp. Code for file Global.asa:
Note: The event handler must be saved in 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 files, for example: Take my machine for example, in absolute terms, global.asa under C:\InetPub\home, and wuf18.asp and wuf19.asp are placed under c:\InetPub\home\asp), the same virtual path allows only one global.asa file to exist.
<script Language=vbscript runat=server>
' Global.asa-wuf18.asp the file name in the download package is 18global.asa and will be renamed when used.
Sub Application_OnStart
' When the first user browses the page, the counter initial value is 0,
' and later, when other users browse, no longer execute the following code
Application.Lock
Application ("Countall") = 0
Application.UnLock
End Sub
</SCRIPT>
Browse wuf18.asp View Run results. It seems that it is not difficult to learn to use only application objects.

Second, know the session object. With the front of the Application object to do bedding, the session object is much easier. It is similar to a Application object, except that it is used only to record information about a single user, which is intended for a single user, so we can use the session object to store information for an individual user.
Like the Application object, the session object also has two events: the Session_OnStart event and the Session_OnEnd event. Use syntax similar to application objects.
It is worth mentioning that before using the Session object, you must confirm that the browser's cookie feature is enabled (the default setting is available).
The following example, file Global.asa requirements above, so the Global.asa in this example will be covered by the Global.asa in this case.
<script Language=vbscript runat=server>
' Global.asa-wuf20.asp in the download package file name is 20global.asa
Sub Session_OnStart
Session ("in") = Now ' function get current time
End Sub

Sub Session_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 "Your time to enter this site:" & Session ("in") & "<Br>"
Response.Write "Please refresh this page in 1 minutes" & "<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 "Browse Time (minutes):" &_
Datediff ("N", Application ("in"), Application ("Out")) & "<Br><Br>"
End If

If IsEmpty ("out") then
Response.Write values for pre-refresh and after session (' out ') are empty ' & ' <Br>
End If
%>
</BODY>
</HTML>
When the user first browses the Web page, the Session_OnStart event occurs, the program records the time when the user enters the Web page, and the Session_OnEnd event does not occur, so the application ("out") value is NULL, the program does not show browse time.
We know that the lifetime of the Application object is the first user browsing the Web page until the server shuts down. The lifetime of the session object is the first time the user browses the Web page (the session begins) until the end of the conversation. So when does the conversation end? In this case, Session.Timeout = 1 Sets the session timeout to 1 minutes, that is, if no request was made to the Web server for up to 1 minutes, the session timed out, causing the session to end, a Session_OnEnd event occurred, and the program executing the code within the Session_OnEnd. Here we place the entry and departure times into application ("in") and application ("out"), because the session object will no longer exist after the conversation, which we validated at the end of the code.
From this routine, you can see that:
1. Application objects and session objects have different lifetimes.
2. The session object is for a single user whose value resides on the client, and the browsing of other users has no effect on that single user's session object. A single user can read and write only the value of his or her session object (for example, session ("in")).
3. Notice how the double quotation marks are represented in Response.Write: ("Out").
4. Special reminder: This program is not practical, only for debugging (only for one user to browse the situation), please consider this is why? See the answer at the end of this article.
Tip: Note the attributes of the Application object.

三、一个 Classic Example: How to display online numbers on a Web page
In the following example, application ("online") is used to store the number of people online, with application ("Countall") 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 visitors 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 here the initial value of application (' Countall ')
Application.UnLock
End Sub

Sub Session_OnStart
Application.Lock
Application ("online") = Application ("online") + 1
Application ("Countall") = Application ("Countall") + 1
Application.UnLock
End Sub

Sub Session_OnEnd
Application.Lock
Application ("online") = Application ("online")-1
Application.UnLock
End Sub

Sub Application_OnEnd
Application.Lock
' should be here to save application (' Countall ') to a file
Application.UnLock
End Sub
</SCRIPT>
The wuf21.asp code is as follows:
<%@ Language=vbscript%>
<HTML>
<BODY>
<%session.timeout = 5%>
<P> Online Number: <%= application ("online")%></p>
<P> Total Visitors: <%= application ("Countall")%></p>
</BODY>
</HTML>
Note that this routine is compared to wuf17.asp, which calculates the number of clicks, and the former is the number of visitors.

Answer: Because the Applicaton object works for all users, browsing by other users can also change the value of application ("in") and application ("out") for multiple users.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.