When using ASP to write virtual communities, online shopping, and other programs, Application and Session objects play an important role. Flexible and rational use of these two objects is the key to improving program quality. The following describes the two built-in ASP objects based on my experience in this area.
1. Application Object member Overview
Application Object members include the set, method, and event of the Application object.
Collections Application object set
Contents set: a set of all variables stored in the Applicaiton OBJECT that are not defined by the <OBJECT> element
StaticObjects: Set of all variables stored in the Application OBJECT defined by the <OBJECT> element
For example, the default. asp has the following values:
application("a")="a"
application("b")=128
application("c")=false
There is a contents set
Application. Contents (1) = "A" 'can also be written as application. Contents ("A") = ""
Application. Contents (2) = 128 'can also be written as application. Contents ("B") = 128
Application. Contents (3) = false' can also be written as application. Contents ("C") = false
We recommend that you use the method such as application. contents ("a") for calling, because it is more intuitive. If you use the serial number to represent it, you should consider the order of values.
Method of Using Application Object
Contents. Remove ("variable name"): removes the specified variable from the Application. Contents set.
Contents. RemoveAll (): delete all variables in the Application. Contents set.
Lock (): Lock the Application object so that only the current ASP page can access the content
Unlock (): Unlock the Application Object
For example, in default. asp:
application("a")="a"
application("b")=128
application("c")=false
response.write application.contents(1)&"<br>"
response.write application.contents(2)&"<br>"
response.write application.contents(3)&"<br>"
response.write "After Remove b:"
application.contents.remove("b")
response.write application.contents(1)&"<br>"
response.write application.contents(2)&"<br>"
Execution result:
A
128
False
After remove B:
A
False
If you want to delete all the variables in the Set, use application. Contents. removeall. As for the lock and Unlock methods, they are often used in practice, and readers are familiar with it, so it is not cumbersome here.
Upload Application Object event
Onstart: occurs when the first Server user visits a page for the first time.
Onend: when the last user's session has ended and all the code of the onend event of the session has been executed, or the last user accesses the server for a period of time (generally 20 minutes) no one will access the server.
To define the onstart and onend events of the Application object, write the code in global. asa file (for example below), and put the file under the root directory of the site (generally inetpub/wwwroot /)
Ii. Session Object member Overview
The Session object has more attributes than the application object, namely, set, attribute, method, and event.
Set of Reset session objects
Contents: a set of all variables stored in a specific session object defined by the <Object> element.
Staticobject: a set of all variables defined by the <Object> element stored in the session object.
For example, the default. asp has the following values:
session("a")="a"
session("b")=128
session("c")=false
There is a contents set
Session. Contents (1) = "A" 'can also be written as session. Contents ("A") = ""
Session. Contents (2) = 128 'can also be written as session. Contents ("B") = 128
Session. Contents (3) = false' can also be written as session. Contents ("C") = false
Attributes of the reset Session Object
CodePage: readable/writable. Integer. Defines the code page used to display page content in a browser. The code page is the numeric value of the character set. Different languages use different code pages. For example, the ANSI code page is 1252, the Japanese code page is 932, and the simplified Chinese code page is 936.
LCID: readable/writable. Integer. Defines the page area ID sent to the browser. LCID is an abbreviation of an international standard that uniquely identifies a region. For example, 2057 defines that the currency symbol of the current region is "currency ".
SessionID: read-only. Long integer. Returns the session ID of the current session. Each time a session is created, the server automatically assigns an identifier. You can determine who the two users first access the server based on their values.
Timeout: readable/writable. Integer. Defines the timeout limit in minutes for sessions. If the user does not refresh or request any webpage within this time period, the session generated by the user ends automatically. The default value is 20.
The above attributes do not play a major role in actual application, and basically do not need to be modified. These attributes are nothing special.
Restore Session Object Method
Contents. Remove ("variable name"): removes the specified variable from the Session. contents set.
Contents. Removeall (): deletes all variables in the Session. contents set.
Abandon (): ends the current user Session and revokes the current Session object.
Contents of the Session object. remove ("variable name") and Contents. the Removeall () method is basically the same as the Application object. for help, you can refer to the above example to change the Application to Session. The difference between Contents. Removeall () and Abandon () is described here. The execution of both methods will release the current
All Session variables of a user Session are different from Contents. removeall () Simply Releases the value of the Session variable without terminating the current Session. Abandon () will terminate the Session and cause the Session_OnEnd event in addition to releasing the Session variable, I hope you will pay attention to the differences between the two.
Events of the reset Session Object
OnStart: triggered when an ASP user session is generated. This event is triggered when any user requests any page of the server.
OnEnd: triggered when the ASP user session ends. This event is also triggered when the Abandon () method or timeout is used.
These two events are the same as the OnStart and OnEnd events of the Application. They must also be stored in the Global. asa file
Focus on the use of these four events.
Iii. Global. asa
The Application and Session objects of ASP reflect the features that other ASP built-in objects do not have-events. Each visitor accesses the server and triggers an OnStart event (the first visitor simultaneously triggers the OnStart event of the Application and Session, but the Application is prior to the Session ), an OnEnd event is triggered at the end of each guest Session (the OnEnd event of the Application and Session is triggered at the end of the last guest Session, but the Session is prior to the Application ).
OnStart and OnEnd events are generally used to count the number of online users in the virtual community, and modify the online offline status of users. To define these two events, you need to write the code in the Global. asa file and put the file in the root directory of the site (/Inetpub/wwwroot/by default /). In addition, the Application and Session objects stipulate that all ASP built-in objects (Response, Request, Server, Session...) except Application objects cannot be used in OnEnd events. The following is an example of how to count the number of online users in a virtual community to show how to use these two events.
File description:
Global. asa is located in the d:/Inetpub/wwwroot/directory.
Default. asp is located in the d:/Inetpub/wwwroot/directory. The logon page of the virtual community is displayed.
Login. asp is located in the d:/Inetpub/wwwroot/directory. It is used to detect the user name and password entered by the user.
Index. asp is located in the d:/Inetpub/wwwroot/directory, the home page of the virtual community
BBS. mdb is located in the D:/inetpub/wwwroot/directory and stores user information in the database
Database (ACCESS) structure:
=== BBS table ===
Id user ID, long integer
Name username, text type
Code password, text type
Online status, yes/no
===global.asa===
<script LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
application("online")=0
End Sub
sub Application_OnEnd
nd Sub
Sub Session_OnStart
End Sub
Sub Session_OnEnd
If session. Contents ("pass") then', determine whether it is the user's session_onend
application.lock
application("online")=application("online")-1
application.unlock
end if
End Sub
</script>
==============
=== Login. asp ===
... 'Password verification, connect to the database, and check whether the user name and password entered by the user are correct
If the password is verified by then
session("name")=rs("name")
session("id")=rs("id")
session("pass")=true
else
rs.close
conn.close
Response. Write "Incorrect password! "
response.end
end if
application.lock
application("online")=application("online")+1
Conn. Execute ("Update BBS Set Online = 1 where id =" & SESSION ("ID") 'sets the user's status to online
application.unlock
rs.close
conn.close
Response. Redirect "index. asp" 'after data is initialized, the page will jump to the Community homepage.
===========
In this example, the application ("online") variable is used to record the number of online users who have logged on to the Community, because once a user accesses the server, the onstart event is generated regardless of whether the user logs on or not, therefore, you cannot add applicaiton ("online") to an onstart event. Because an onend event is generated no matter whether the Session of the logged-on user ends. (If a visitor accesses the server but does not log on to the Community, an onend event is generated after the session ends ), therefore, an if statement is used in the session_onend event to determine whether the event is an onend event of a logged-on user. If so, the number of online users is reduced by one.
This is just a simple example of counting the number of online users. For a complete virtual community, it is not enough to count only the number of online users, in this example, the online field in the database is used to record the online status of the user. in ASP, online is set to 1, but online is not set to 0 when the user is offline. To improve it, modify the session_onend event and set online to 0 in this event.
===global.sas===
<script LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
application("online")=0
set application("conn")=Server.CreateObject("ADODB.Connection")
Application ("DB") = server. mappath ("/BBS. mdb") 'it is best to use the absolute path/BBS. mdb, which is described in detail below.
End Sub
sub Application_OnEnd
set application("conn")=nothing
End Sub
Sub Session_OnStart
End Sub
Sub Session_OnEnd
If session. Contents ("pass") then', determine whether it is the user's session_onend
application("con").open ="driver={Microsoft Access Driver (*.mdb)};dbq="&application("db")
application.lock
application("online")=application("online")-1
application("con").Execute ("update friends set online=0 where id="&session.contents("id"))
application.unlock
application("con").close
end if
End Sub
</script>
==============
So far, the complete code has been completed. Because the Server object cannot be used in the OnEnd events of the Application and Session, you need to connect the database and the physical address of the database on the Server (d:/inetpub/wwwroot/bbs. mdb) is stored in the application variable and pre-processed in the Application_OnStart event. Similarly, session ("pass") cannot be used in Session_OnEnd events to replace session. contents ("pass") (The following is a detailed description ).
4. Two points worth attention in this article
Session. contents in the OnEnd event
Friends who are new to global. asa often use
If session. Contents ("pass") then is written
if session("pass") then,
In this case, the system will not prompt an error, but will never execute the content after then. This is because the Session object is forbidden in the OnEnd event, but the Session variable can be called using the set of session objects. Because IIS does not prompt any error information, I have wasted a lot of time on it. I hope you will learn it!
In the ⒉ Application_OnStart event, when you use Server. MapPath to obtain the physical address of the database, you must use the absolute address. To illustrate this problem, you can perform an experiment:
Application ("DB") = server. mappath ("/BBS. mdb") changed:
application("db")=Server.MapPath("bbs.mdb")
Create a test subdirectory in the d:/inetpub/wwwroot/directory and write a temp. asp file in the test directory.
====test.asp====
<%response.write application("db")%>
================
Copy temp. asp to the root directory (d:/inetpub/wwwroot /). Use NotePad to open global. asa and then open two browsers. In browser A, enter the address http: // localhost/temp. asp and press enter to output the address in the browser:
d:/inetpub/wwwroot/bbs.mdb
Then, click the "file" menu in the notepad window and select "save" (make global. the modification time of the asa changes so that IIS can restart all services), and then enter the address http: // localhost/test/temp in browser B. asp, press enter, and the output on the browser is:
d:/inetpub/wwwroot/test/bbs.mdb
Global. although the ASA file is stored in the root directory of the site. in mappath, the relative address is used. If the page accessed by the user who triggers the application_onstart event for the first time does not belong to the root directory, the physical address of the database will not be the expected result, I hope you will be very careful.