When using ASP to write virtual communities, online shopping and other programs, the Application and Session objects play a decisive role, and they can be used flexibly and rationally.
This object is the key to improving program quality. Let me give you an in-depth introduction to these two built-in objects of ASP based on my experience in this area.
1. Overview of members of the Application object
Application object members include Application object collections, methods, and events.
⒈A collection of Application objects
Contents collection: A collection of all variables stored in the Applicaiton object that are not defined using the <OBJECT> element.
StaticObjects: A collection of all variables stored in the Application object defined using the <OBJECT> element
Example: There is the following assignment in default.asp
application(a)=a
application(b)=128
application(c)=false
Then there is the contents collection
application.contents(1)=a 'can also be written as application.contents(a)=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
The author here recommends that you use a method such as application.contents(a) when calling, because this is more intuitive. If it is represented by a serial number,
Consider the order of assignment.
⒉Methods of Application object
Contents.Remove(variable name): Delete the specified variable from the Application.Contents collection
Contents.RemoveAll(): Delete all variables in the Application.Contents collection
Lock(): Lock the Application object so that only the current ASP page can access the content.
Unlock(): Unlock the Application object
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 variables in the collection, just use application.contents.removeall. As for the Lock and Unlock methods, they are often used in practice. Read
The reader is also familiar with it, so it won’t be a burden here.
⒊Application object event
OnStart: Occurs when the first user accessing the server accesses a page for the first time
OnEnd: Occurs when the last user's session has ended and all codes for the session's OnEnd event have been executed, or when the last user accesses
The server occurs after a period of time (usually 20 minutes) when no one accesses the server.
If you want to define what to do in the OnStart and OnEnd events of the application object, you need to write the code in the Global.asa file (examples below), and
And place the file in the root directory of the site (usually Inetpubwwwroot)
2. Overview of the members of the Session object
The members of the Session object have one more attribute than the Application object, namely: collections, properties, methods, and events.
⒈Collection of Session objects
Contents: A collection of all variables stored in a specific Session object that are not defined using the <OBJECT> element.
StaticObject: A collection of all variables defined using the <OBJECT> element and stored in the Session object.
Example: There is the following assignment in default.asp
session(a)=a
session(b)=128
session(c)=false
Then there is the contents collection
session.contents(1)=a 'can also be written as session.contents(a)=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
⒉Properties of Session object
CodePage: Readable/Writable. Integer type. Defines the code page used to display page content in the browser. A code page is the character set of numeric values that 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 type. Defines the page locale identifier sent to the browser. LCID is an international standard abbreviation that uniquely identifies a region, e.g.
2057 defines the currency symbol of the current region as £.
SessionID: Read only. Long type. Returns the session identifier for this session. Each time a session is created, an identifier is automatically assigned by the server. can be based on it
The value determines which of the two users accesses the server first.
Timeout: Readable/Writable. Integer type. Defines a timeout limit in minutes for the session. If the user does not refresh or request any one within this time
web page, the session generated by the user will automatically end. The default value is 20.
The above attributes are of little use in actual applications, and basically do not need to be modified. There is nothing special about these attributes.
⒊Methods of Session object
Contents.Remove(variable name): Remove the specified variable from the Session.contents collection
Contents.Removeall(): Remove all variables in the Session.contents collection
Abandon(): End the current user session and destroy the current Session object.
The Contents.Remove (variable name) and Contents.Removeall() methods of the Session object are basically the same as those of the Application object.
To help understand, you can refer to the above example to change Application to Session. What I want to explain here is Contents.Removeall() and Abandon()
The difference is that executing these two methods will release the current
All Session variables of the user session. The difference is that Contents.Removeall() simply releases the value of the Session variable without terminating the current session.
In addition to releasing the Session variable, Abandon() will also terminate the session and trigger the Session_OnEnd event. I hope everyone will pay attention to the difference between the two.
⒋Events of Session object
OnStart: Triggered when an ASP user session is generated. This event is generated once any user requests any page from this server.
OnEnd: Triggered when the ASP user session ends. This event will also be triggered when the Abandon() method or timeout is used.
These two events are the same as the OnStart and OnEnd events of Application and must be placed in the Global.asa file.
Let’s focus on studying the use of these four events with you.
3. Global.asa
ASP's Application and Session objects embody a feature that other ASP built-in objects do not have - events. Every time a visitor accesses the server, a
OnStart events (the first visitor will trigger the OnStart events of Application and Session at the same time, but Application precedes Session), each visitor
An OnEnd event will be triggered when each session ends (the OnEnd events of both Application and Session will be triggered when the last guest session ends, but
Session precedes Application).
The two events OnStart and OnEnd are generally used in virtual communities to count the number of people online, modify the online and offline status of users, etc. To define these two things specifically
file, you need to write the code in the Global.asa file and place the file in the root directory of the site (the default is Inetpubwwwroot). in addition,
The Application and Session objects specify other ASP built-in objects (Response, Request,
Server, Session...) cannot be used. The following is an example of counting the number of people online in a virtual community to illustrate how to use these two events.
File description:
global.asa is located in the d:Inetpubwwwroot directory
default.asp is located in the d:Inetpubwwwroot directory, the virtual community login page
login.asp is located in the d:Inetpubwwwroot directory and is used to detect the user name and password entered by the user.
index.asp is located in the d:Inetpubwwwroot directory, the homepage of the virtual community
bbs.mdb is located in the d:Inetpubwwwroot directory and is a database that stores user information.
Database (ACCESS) structure:
===bbs table===
id user ID, long integer
name username, text type
code password, text type
online online status, yes/no
===global.asa===
<script LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
application(online)=0
End Sub
subApplication_OnEnd
nd Sub
SubSession_OnStart
End Sub
SubSession_OnEnd
if session.contents(pass) then 'Determine whether it is the Session_OnEnd of the logged in user
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 password verification passes then
session(name)=rs(name)
session(id)=rs(id)
session(pass)=true
else
rs.close
conn.close
response.write password is wrong!
response.end
end if
application.lock
application(online)=application(online)+1
conn.Execute (update bbs set online=1 where id=&session(id))'Set the user's status to online
application.unlock
rs.close
conn.close
response.redirect index.asp 'After initializing the data, jump to the community homepage
===========
In this example, use the application (online) variable to record the number of online people who have logged in to the community, because once a user accesses the server, it does not matter whether the user is
When you log in, the OnStart event will be generated, so you cannot increase Applicaiton (online) by one in the OnStart event. Because regardless of whether it is a logged-in user's session
The OnEnd event will be generated when it ends (if a visitor visits the server but does not log in to the community, the OnEnd event will also be generated after his session ends), so in
An if statement is used in the Session_OnEnd event to determine whether it is an OnEnd event for a logged-in user. If so, the number of people online will be reduced by one.
This is just a simple example of counting the number of people online. For a complete virtual community, it is not enough to just count how many people are online. In this case, counting
There is an online field in the database that is used to record the user's online status. When the user logs in, online is set to 1 in login.asp, but it does not appear when the user is offline.
Online is set to 0. To improve it, you need to modify the Session_OnEnd event and set online to 0 in this event.
===loal. sas===
<script LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
application(online)=0
set application(conn)=Server.CreateObject(ADODB.Connection)
application(db)=Server.MapPath( bs.mdb) 'It is best to use the absolute path bs.mdb here, as detailed below
End Sub
subApplication_OnEnd
set application(conn)=nothing
End Sub
SubSession_OnStart
End Sub
SubSession_OnEnd
if session.contents(pass) then 'Determine whether it is the Session_OnEnd of the logged in user
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>
==============
At this point, the complete code has been completed. Because the Server object cannot be used in the OnEnd event of Application and Session, the database's
The physical address of the connection and database on the server (d:inetpubwwwroot bs.mdb) is stored in the application variable and
Preprocessed in Application_OnStart event. In the same way, session(pass) cannot be used instead of session.contents in the Session_OnEnd event.
(pass) (detailed explanation below).
4. Two points worthy of attention in the examples in this article
⒈session.contents in the OnEnd event
Friends who have just started to contact global.asa often refer to the above Session_OnEnd event
if session.contents(pass) then written as
if session(pass) then,
In this case, the system will not prompt an error, but the content after then will never be executed. This is because the Session object is prohibited from being used in the OnEnd event.
But session variables can be called using a collection of Session objects. Because IIS did not prompt any error message, the author once wasted a lot of time on this.
between. I hope everyone can learn from this!
⒉When using Server.MapPath to obtain the physical address of the database in the Application_OnStart event, the absolute address should be used. To illustrate this problem, you can
Let’s do an experiment: change the above Application_OnStart event
application(db)=Server.MapPath( bs.mdb) is changed to:
application(db)=Server.MapPath(bbs.mdb)
Then create a test subdirectory in the d:inetpubwwwroot directory and write a temp.asp in the test directory.
====test.asp====
<%response.write application(db)%>
================
Then copy temp.asp and place it in the root directory (d:inetpubwwwroot). Open global.asa with notepad, and then open two browsers, browser A
Enter the address http://localhost/temp.asp and press Enter. The following will be output on the browser:
d:inetpubwwwroot bs.mdb
Then, click the File menu on the Notepad window and select Save (to change the modification time of global.asa, thereby causing IIS to restart all services), and then click
Enter the address http://localhost/test/temp.asp in browser B and press Enter. The output on the browser is:
d:inetpubwwwrootestbs.mdb
Although the global.asa file is placed in the site root directory, if a relative address is used in server.mappath, Application_OnStart is triggered.
If the page visited by the event user for the first time does not belong to the root directory, obtaining the physical address of the database will not be the expected result. I hope everyone will be particularly careful.