Study Application and Session target deep
Tuesday, March 03, 2009 by rain
one, the member overview of Application object
The gather that member of object of Application includes Application boy or girl friend, method and incident.
The gather of object of Application of ⒈
Contents gather:The memory that defines without use %26lt;OBJECT%26gt; element all variable gather in Applicaiton object
?of StaticObjects:The memory that element of OBJECT%26gt; of ?lt; of A Chinese-style unlined garment defines all variable gather in Application object
exemple:Next assignment are like in Default.asp
Application("a" )= "a "
Application("b" )=128
Application("c" )=false
has Contents rally
Application.contents(1)= "a" ' also can be written for Application.contents("a" )= "a "
Application.contents(2)=128 ' also can be written for Application.contents("b" )=128
Application.contents(3)=false ' also can be written for Application.contents("c" )=false
recommends you to use kind of method that is like Application.contents("a" ) when call in this author,Because such more intuitionistic,If use what if serial number will express, should consider to assign early or late ordinal.
The method of object of Application of ⒉
Contents.Remove(" is variable renown " ) :Delete designation variable from inside Application.Contents gather
Contents.RemoveAll() :All variable in assembling Application.Contents are deleted
Lock() :The lock decides Application object,Make current ASP page only right content can have a visit
Unlock() :Remove to be decided to the lock of Application object
exemple:In Default.asp:
Application("a" )= "a "
Application("b" )=128
Application("c" )=false
Response.write Application.contents(1)%26amp; %26quot;%26lt;br%26gt; %26quot;
Response.write Application.contents(2)%26amp; %26quot;%26lt;br%26gt; %26quot;
Response.write Application.contents(3)%26amp; %26quot;%26lt;br%26gt; %26quot;
Response.write "After Remove B:"
Application.contents.remove("b" )
Response.write Application.contents(1)%26amp; %26quot;%26lt;br%26gt; %26quot;
Response.write Application.contents(2)%26amp; %26quot;%26lt;br%26gt; %26quot;
carries out a result:
A
128
False
After Remove B:
A
False
If wants to delete all variable in gather to use Application.contents.removeall can,Be in as to Lock and Unlock method actual in classics is commonly used to,Read
Person more familiar also,Be absent here burdensome.
Incident of object of Application of ⒊
OnStart:Visit of the first user first time that visits a server happens when some page
OnEnd:The conversation when the last user has ended and all code had carried out this conversational OnEnd incident after ending, happen,Or visit of the last user
Server period of time (it is 20 minutes commonly) hind still visit this server to arise without the person.
What to do to need to write code in Global.asa in the OnStart that wants to define Application target and OnEnd incident in this file (later development has give an example) ,And
And below the root list that puts this document in the site (it is Inetpub\wwwroot\ commonly)
two, the member overview of Session object
The member of object of Session is more than Application object an attribute,Namely:Gather, attribute, method, incident
The gather of object of Session of ⒈
Contents:The memory that defines without use %26lt;OBJECT%26gt; element all variable gather at specific Session object.
StaticObject:Use %26lt;OBJECT%26gt; element defines, the memory gather at all variable in Session object.
exemple:Next assignment are like in Default.asp
Session("a" )= "a "
Session("b" )=128
Session("c" )=false
has Contents rally
Session.contents(1)= "a" ' also can be written for Session.contents("a" )= "a "
Session.contents(2)=128 ' also can be written for Session.contents("b" )=128
Session.contents(3)=false ' also can be written for Session.contents("c" )=false
The attribute of object of Session of ⒉
CodePage: Readable / can write.Whole model.The code page that the definition uses at page content showing in the browser.The number that code page is character set is worth,Different language is used
Different code page.For example,ANSI code page is 1252,Japanese code page is 932,Page of simplified Chinese code is 936.
LCID: Readable / can write.Whole model.The page area that definition hair gives a browser marks.The abbreviate of an international standard that LCID is exclusive label area,For example,
The 2057 monetary symbols that define current area are " £ " .
SessionID: Read-only.Long whole model.The conversational identifier that returns this conversation.Every found a conversation,Allocate an identifier automatically by the server.Can according to it
the value judges two users is who visits a server first.
Timeout: Readable / can write.Whole model.Define for conversation with minute the overtime demarcate that is an unit.If the user is in this to there is refresh inside time or request any
Webpage,Criterion the conversation that this user produces ends automatically.Default value is 20.
Above attribute is in actual application effect is not big,And basically do not need how to be revised,These a few attribute also the place with special that's all right.
The method of object of Session of ⒊
Contents.Remove(" is variable renown " ) : Delete designation variable from inside Session.contents gather
Contents.Removeall() : Delete all variable in Session.contents gather
Abandon() : End current user conversation and cancel is current Session object.
The Contents.Remove(" of object of Session is variable of renown " ) and Contents.Removeall() method and Application object basically it doesn't matter is distinguished,For
The help understands,Everybody can consult the example above Application instead Session.What the specification wants here is Contents.Removeall() and Abandon()
distinguish,Carry out these two methods to be able to be released current
All Session of conversation of user are variable,Different is Contents.Removeall() be worth and do not end current conversation what release Session variable purely,And
Abandon() still can stop conversation to cause Session_OnEnd incident besides releasing Session variable,Hope everybody notices both distinction.
The incident of object of Session of ⒋
OnStart: Spark when ASP user conversation arises,Once either user requests either page to this server,produce this event namely.
OnEnd: Spark when ASP user conversation ends,Should use Abandon() method or also can spark overtime this incident.
These two incident mix episode of the OnStart of Application, OnEnd is same,Also be to must be put in Global.asa file,Below
The face studies with respect to key and everybody the use of these four incident.
three, Global.asa
The Application of ASP and Session object reflected the feature that place of object of the buy inside other ASP does not have- - incident.The metropolis when each caller visits a server sparks one
OnStart incident (the first caller can spark at the same time the OnStart incident of Application and Session,But Application first at Session) ,Every caller
the metropolis when conversational end sparks an OnEnd incident (the meeting when conversation of the last caller ends sparks at the same time the OnEnd incident of Application and Session,But
Session first at Application) .
These two incident apply OnStart and OnEnd in the statistic in fictitious community commonly the online condition leaving a line of online number, modification user.Want specific definition these two things
,Need writes code in Global.asa file,Below the root list that puts this document in the site (default is \Inetpub\wwwroot\ ) .Additional,
The object set Application and Session to be in OnEnd incident besides Application object object of the buy inside other ASP (Response, Request,
Server, Session. . . ) entirely cannot be used.Raise count of a fictitious community below the case of online number will explain how to use these two incident.
file explains:
Global.asa is located in D:\Below Inetpub\wwwroot\ catalog
Default.asp is located in D:\Below Inetpub\wwwroot\ catalog,Fictitious community logs onto a page
Login.asp is located in D:\Below Inetpub\wwwroot\ catalog,With the user name that at detecting the user inputs and password
Index.asp is located in D:\Below Inetpub\wwwroot\ catalog,Fictitious community home page
Bbs.mdb is located in D:\Below Inetpub\wwwroot\ catalog,The database of memory user information
database (ACCESS) structure:
===Bbs watch===
ID of user of Id,Long whole model
Name of user of Name,Text
Password of Code,Text
Online condition of Online,Be / deny
===Global.asa===
> of Script LANGUAGE= "VBScript" RUNAT= "Server" of <
Sub Application_OnStart
Application("online" )=0 of
End Sub
Sub Application_OnEnd
Nd Sub
Sub Session_OnStart
End Sub
Sub Session_OnEnd
If Session.contents("pass" ) Then ' of judges the Session_OnEnd that whether is entry user
Application.lock of of
Application("online" )=application("online" )-1 of of
Application.unlock of of
End If of
End Sub
< / Script >
==============
===Login.asp===
. . . . . . ' password test and verify,Join database,Detect the user name that the user inputs and code are right
Test and verify of password of If passes Then
Session("name" )=rs("name" of )
Session("id" )=rs("id" of )
Session("pass" )=true of
Else
Rs.close
Conn.close of
Mistake of password of Response.write " of !Mistake of password of Response.write " of !
Response.end of
End If
Application.lock
Application("online" )=application("online" )+1
Conn.Execute ("update Bbs Set Online=1 Where Id=" %26session("id" ))' sets the condition of the user for online
Application.unlock
Rs.close
Conn.close
Jump after data of initialization of Response.redirect "index.asp" ' turn to community home page
===========
is in this exemple,The online number that with variable of Application("online" ) the record has logged onto community,Because once have an user,visit a server and no matter whether the user logins,Incident of metropolis generation OnStart,Cannot make in OnStart incident Applicaiton("online" ) is added so one.Because whether be the conversational end of entry user without giving thought to,can produce OnEnd event (if caller visited a server but do not log onto community,OnEnd event also can produce after his conversation ends) ,Used an If statement to judge the OnEnd event that whether records an user to already was ascended in Session_OnEnd incident so,If be ability,will online number is decreased one.
is a statistic this the simple case of online number,To a whole fictitious community,Mere count has how many person online it is insufficient,There is an Online field in the database in this exemple is with the online condition that will record an user,When the user logins,Set Online in Login.asp for 1,But when the user leaves a line, did not set Online for 0,Should perfect it,Be about to revise Session_OnEnd incident,Set Online in this incident for 0.
===. of l of a of b of g l os a s===
> of Script LANGUAGE= "VBScript" RUNAT= "Server" of <
Sub Application_OnStart
Application("online" )=0 of
Set Application("conn" )=Server.CreateObject("ADODB.Connection" of )
Here of Application("db" )=Server.MapPath("\bbs.mdb" ) ' of had better use absolutely method \bbs.mdb,Later development has detailed introduction
End Sub
Sub Application_OnEnd
Set Application("conn" )=nothing of
End Sub
Sub Session_OnStart of
End Sub
Sub Session_OnEnd
If Session.contents("pass" ) Then ' of judges the Session_OnEnd that whether is entry user
Application("con" ).open = "driver={Microsoft Access Driver (*.mdb)};dbq=" %26application("db" of of of )
Application.lock of of of
Application("online" )=application("online" )-1 of of of
Application("con" ).Execute ("update Friends Set Online=0 Where Id=" %26session.contents("id" ) of of of )
Application.unlock of of of
Application("con" ).close of of of
End If of
End Sub
Script > of / of <
==============
so far,Whole code had been finished.Because be in the OnEnd incident of Application and Session,cannot use Server target,Want so the database
The physical address that join reachs a database to go up in the server (D:\Inetpub\wwwroot\bbs.mdb) memory is in Application variable,Be in
The foreclose in Application_OnStart incident.With manage,Cannot replace Session.contents in Session_OnEnd incident with Session("pass" )
("pass" ) (have detailed specification below) .
four, two bits when bring to sb's attention deserves in article example
The Session.contents in incident of OnEnd of ⒈
The friend classics that just began to contact Global.asa above regular meeting general in Session_OnEnd incident
of If Session.contents("pass" ) Then is written into
If Session("pass" ) Then,
Such word the system won't hint mistake,But also won't carry out the content at the back of Then forever,Because,this is in object of Session of the taboo in OnEnd incident,
But can call Session variable with the gather of Session object.Because IIS does not have clew any wrong message,So the author once was above this a lot of more wasteful when
.In this hope everybody is brought think ancient bronze mirror!
Absolutely address should be used when the physical address that the database gets with Server.MapPath in incident of Application_OnStart of ⒉ to show this issue,Everybody but
In order to do a test:Will above in Application_OnStart incident
Instead of Application("db" )=Server.MapPath("\bbs.mdb" ) :
Application("db" )=Server.MapPath("bbs.mdb" )
Be in next D:\Subdirectory of a Test is built below Inetpub\wwwroot\ catalog,Write a Temp.asp to be in Test catalog.
====Test.asp====
> of % of Response.write Application("db" ) of % of <
================
Copy Temp.asp to be put below root catalog again (D:\Inetpub\wwwroot\ ) .Open Global.asa with notebook,Open two browsers again,Browser A inputs address Http://localhost/temp.asp,By carriage return,Will output on the browser:
D:\Inetpub\wwwroot\bbs.mdb
Next,In menu of " of file of " of the dot on the window of notebook,Choose " to save " (make the modification time of Global.asa is changed,Make IIS starts all services again thereby) ,Input address Http://localhost/test/temp.asp in browser B again,By carriage return,What output on the browser is:
D:\Inetpub\wwwroot\test\bbs.mdb
Although Global.asa file is to be put below site root catalog,But if those who use is in in Server.mappath,be opposite address,And spark the page that the user first time of Application_OnStart incident visits is not the word that belongs to a catalog,The result that gets the physical address of the database will not be expectation,Hope everybody wants special caution.