In-depth study of application and session objects

Source: Internet
Author: User

In the process of compiling virtual community and online shopping with ASP, application and session objects play an important role, and the ability to use these two objects flexibly and rationally is the key to improve the quality of the program. The following let the author according to their own experience in this area, to everyone in-depth introduction of the ASP of the two built-in objects.

  I. Overview of members of application objects

Application object members include collections, methods, and events for application objects.

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: In Default.asp, there are the following assignments

Application ("a") = "a"

Application ("B") =128

Application ("C") =false


Then there are contents collections

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


In this paper, I recommend that you use the method of class such as application.contents ("a") when calling, because it is more intuitive, if you use the ordinal to indicate the order of the assignment should be considered. Methods for ⒉application objects

Contents.Remove ("Variable name"): Removes the specified variable from the Application.Contents collection

Contents.RemoveAll (): Remove all variables from the Application.Contents collection

Lock (): Locks 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 the variables in the collection with Application.contents.removeall, as for the lock and unlock methods are often used in practice, the reader is also familiar with, here is not cumbersome.

⒊application Object Event OnStart: Occurs when the first Access server user accesses a page for the first time

OnEnd: Occurs when the last user's session has ended and the OnEnd event of the session has been completed after all code has been executed, or the last user accesses the server for a period of time (typically 20 minutes) and still no one has access to the server.

What you need to do to define the OnStart and OnEnd events of the Application object is to write the code in the Global.asa file (example below) and place the file at the root of the site (typically inetpub\wwwroot\)

  Ii. Overview of the members of the Session object

A member of a session object has one more property than the Application object, which is a collection of collections, properties, methods, events ⒈session objects

Contents: There is no collection of all variables stored in a specific session object defined by the <OBJECT> element.

Staticobject: A collection of all the variables that are defined with the <OBJECT> element and stored in the session object.

Example: In Default.asp, there are the following assignments

Session ("a") = "a"

Session ("B") =128

Session ("C") =false


Then there are contents collections

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


⒉session properties of an object

CodePage: readable/writable. Integral type. Defines the code page that is used to display the page content in the browser. A code page is a numeric value of a character set, and 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. Integral type. Defines the page region ID that is sent to the browser. The LCID is an international standard abbreviation that uniquely identifies a region, for example, 2057 defines the currency symbol for the current region as "£".

SessionID: Read only. Long integer type. Returns the session identifier for this session. Each session created is automatically assigned an identifier by the server.   Depending on its value, you can determine two users who first access the server. Timeout: readable/writable. Integral type. Defines the time-out limit in minutes for a session. If the user does not refresh or request any of the pages within this time, the session generated by the user automatically ends. The default value is 20.

The above attribute is not very useful in practical application, and basically does not need how to modify, these properties also have no special place.

Methods for ⒊session objects

Contents.Remove ("Variable name"): Removes the specified variable from the Session.Contents collection

Contents.RemoveAll (): Delete all variables in the Session.Contents collection

Abandon (): Ends the current user session and undoes the current session object.

The Contents.Remove ("variable name") and Contents.RemoveAll () methods of the Session object are basically no different from the Application object, to help understand, We can refer to the example above to change the application to session. Here to illustrate the difference between contents.removeall () and abandon (), both methods will release the current

All session variables for user sessions, except that Contents.RemoveAll () simply releases the value of a session variable without terminating the current session, while abandon () terminates the session throw Session_ OnEnd event, I hope you notice the difference between the two.

Events for ⒋session objects

OnStart: Triggered when an ASP user session is generated, this event occurs whenever any user requests any page from the server.

OnEnd: Triggered when the ASP user session ends, the event is triggered when the abandon () method or timeout is used.

These two events, like the OnStart and OnEnd events of application, must also be placed in the Global.asa file, under
We will focus on the use of these four events.

Third, Global.asa

The application and session objects of ASP embody the features that other ASP built-in objects don't have-events. A onstart event is triggered every time a guest accesses the server (the first visitor triggers the application and session OnStart events, but application precedes the session), A OnEnd event is triggered at the end of each guest session (at the end of the last guest session, both the application and session OnEnd events are triggered, but the session precedes application).

OnStart and onend are typically used to count online numbers in a virtual community, to modify a user's online offline status, and so on. To specifically define these two events, 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 \inetpub\wwwroot\). In addition, the application and Session objects specify other ASP built-in objects (Response, Request, Server, Session ...) in the OnEnd event, in addition to the Application object. ) is not available. Here is an example of how to use these two events in the virtual Community statistics online number.

File Description:

Global.asa in the D:\Inetpub\wwwroot\ directory

Default.asp in the D:\Inetpub\wwwroot\ directory, the virtual Community login page

Login.asp is located in the D:\Inetpub\wwwroot\ directory to detect user input user name and password

Index.asp is located in the D:\Inetpub\wwwroot\ directory, the virtual community home

Bbs.mdb in the D:\Inetpub\wwwroot\ directory, the database where the user information is stored

Database (ACCESS) Structure:

===bbs table = = =

ID user ID, long integer

Name User name, text type

Code password, text type

Online status, yes/No

===global.asa===

==============


===login.asp===

... ' Password authentication, connect to the database, detect the user name and password is correct

If password is verified through then

Session ("name") =rs ("name")

Session ("id") =rs ("id")

Session ("Pass") =true

Else

Rs.close

Conn.close

Response.Write "Password Error! "

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" ' Initialize data and jump to Community homepage

===========


In this example, the application ("online") variable is used to record the number of online people who have logged in to the community, because once a user accesses the server regardless of whether the user is logged in, the OnStart event is generated, so applicaiton cannot be made in the OnStart event (" Online ") plus one. Because the OnEnd event occurs regardless of whether a logged-on user's session ends (if a guest accesses the server but does not log in to the community, the OnEnd event occurs at the end of his session), so the Session_ The OnEnd event uses the IF statement to determine whether the OnEnd event is a logged-on user, and if so, to reduce the number of people online.

This is just a simple example of statistics online, for a complete virtual community, it is not enough to count the number of people online, in this case there is a online field in the database to record the user's status, when the user logs in, In Login.asp the online is set to 1, but the user offline when the online is not set to 0, to improve it, it is necessary to modify the Session_OnEnd event, in this event, the online set to 0.

===global. sas===

==============


At this point, the complete code is complete. Because the server object cannot be used in the OnEnd event of the application and session, the connection to the database and the physical address of the database on the server (d:\inetpub\wwwroot\ Bbs.mdb) is stored in the application variable and is pre-processed in the Application_OnStart event. Similarly, you cannot use the session ("pass") in the Session_OnEnd event to replace the session.contents ("Pass") (described below). Shanghai Treatment Impotence Hospital}

  Iv. two points worth noticing in this case

The session.contents in the ⒈onend incident.

Friends who have just begun to contact Global.asa will often session_onend the above-mentioned events.


If Session.Contents ("pass") then write

If session ("pass") then,


In this case, the system will not prompt for errors, but will never do what follows then, because it is forbidden to use the session object in the OnEnd event, but you can invoke the session variable with the collection of Session objects. Because IIS does not prompt any error messages, I have wasted a lot of time on this. In this hope everyone lesson!

⒉application_onstart event when using Server.MapPath to obtain the physical address of the database should use absolute address in order to illustrate this problem, you can do an experiment: the above Application_OnStart event

Application ("DB") =server.mappath ("\bbs.mdb") should read:

Application ("DB") =server.mappath ("Bbs.mdb")


Then create a test subdirectory under the D:\inetpub\wwwroot\ directory and write a temp.asp in the test directory.

====test.asp====

<%response.write application ("DB")%>

================


Place the temp.asp copy in the root directory (d:\inetpub\wwwroot\). Open Global.asa with Notepad, then open two browsers, browser a input address http://localhost/temp.asp, press ENTER, will be output on the browser:

D:\inetpub\wwwroot\bbs.mdb


Then, in the Notepad window point "File" menu, choose "Save" (make Global.asa change time, so that IIS restart all services), and then enter the address in browser b http://localhost/test/temp.asp, press ENTER, the output on the browser is:

D:\inetpub\wwwroot\test\bbs.mdb


The Global.asa file is placed at the root of the site, but if you are using a relative address in Server.MapPath, and the first time the user who triggered the Application_OnStart event does not belong to the root directory, Getting the physical address of the database will not be the desired result, I hope you have to be particularly careful.

In-depth study of application and session objects

Related Article

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.