Preliminary understanding of Application object, Session object, cookie object, server object

Source: Internet
Author: User

Application objects: Objects that record application parameters

Used to share application-level information, where multiple users share a Application object. When the first user requests an ASP. NET file, the application is started and the Application object is created. Once the Application object is created, it can share and manage information for the entire application. The Application object persists until the application shuts down. Therefore, the Application object is the primary object that is used to start and manage your ASP.

Property:

Application.allkeys: Returns all Application object variable names into a string array

Application.count: Gets the number of application object variables

Application.item: Allows the use of indexes or application variable names to return content values

application["App1"]= "App1"; application[0]//assigning values to Application object variables

application["App2"]= "APP2"; APPLICATION[1]

application["App3"]= "APP3"; APPLICATION[2]

Method:

Application.add ("App1", "App1");//Add a Application variable

Application.clear ();//Clear all Application object variables

Application.Lock ();//Lock all Application object variables

Application.remove ("App1");//Remove a Application object variable using the variable name

Application.removeall ();//Remove all Application object variables

Application.UnLock ();//Unlocked Application object variable

Common events:

1. Application_Start Events

The Application_Start event occurs before the first time a new session (that is, an event) is created, and only application and server built-in objects can be used. Referencing the session, request, or response object in the Application_Start event will result in an error. Since the Application object is shared by multiple users, it is fundamentally different from the session object, while the Application object does not disappear due to the departure of one or even all of the users, once the Application object is established, It will always be there until the site is closed or the Application object is unloaded, which usually takes a long time. Because application objects do not log off themselves after they are created, you must use them with special care. In addition, it takes up memory and avoids slowing down the server's responsiveness to other work. Abort Application object has 3 methods, respectively, the service is aborted, the Global.asax file is changed, or the Application object is unloaded

2. Application_End Events

The Application_End event occurs after the application exits with the Session_End event, and only application and server built-in objects are available. The Application_End event is triggered only if the service is aborted or the Application object is unloaded, and if the Application object is used alone, the event can be triggered by the Application object when it is unloaded with the Unload event. A Application_End event must have occurred after the Session_End event. The Application_End event triggers the only script that exists in the Global.asax file

* You can write code in the Application_End event if you want the service to abort or to perform some action when the Application object is unloaded

Application applications:

Achieve online headcount statistics

1Void Session_Start (Objectsender, EventArgs e)2 {3Application.Lock ();//Lock4application["Count"] = (int) application["Count"] +1;//Visitor Increase5Application.UnLock ();//Unlock6 }7 8Void Session_End (Objectsender, EventArgs e)9 {TenApplication.Lock ();//Lock Oneapplication["Count"] = (int) application["Count"] -1;//Visitor Reduction AApplication.UnLock ();//Unlock -}
View Code

Session object: Record the variable object on the browser side

A variable or object used to store a cross-Web program. The session object is for a single Web user, which means that the server assigns its session object to the connected client, and the client cannot access each other. When the session object exceeds the set valid time, the session object disappears. Both the session object and the Application object are members of the Page object and can therefore be used directly in the Web page.

Property:

Session.Contents: Gets a reference to the current session-state object

Item: Gets or sets the session value

TimeOut: Sets the effective time for the session object, with a default value of 20 minutes You can set the Timeout property in the application's Web. config file by using the Timeout property of the sessionstate configuration element, or you can set the Timeout property value directly using program code

Method:

Abandon: This method ends the current session and clears all information in the session

Add: For adding a new item to the Session object collection

CopyTo: Copies a collection of session-state values into a one-dimensional array (starting at the specified index of the array)

Clear: This method clears all session object variables, but does not end the conversation

Session Application:

Log User name and password

session["UserName"] = UserName;

session["PassWord"] = PassWord;

Preliminary understanding of Application object, Session object, cookie object, server object

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.