ASP Basics Tutorial: ASP built-in object application and session

Source: Internet
Author: User
Tags terminates

In the previous article, the author gives you a detailed introduction to the ASP built-in object Response use method, in this article the author will continue to introduce another two very useful and important ASP's built-in object application and Session.

In addition to the objects used to send, receive, and process data in an ASP's built-in object, there are some very useful objects that represent Active Server applications and individual user information.

Let's take a look at the Application object first. All. asp files in the same virtual directory and its subdirectories form the ASP application. Instead of using the Application object, we can share information among all users of a given application and persist the data while the server is running. Also, the Application object has a way to control access to application-tier data and events that can be used to trigger the process when the application starts and stops.

Now let's learn application objects together.

   First, the attribute

Although the Application object does not have built-in properties, we can use the following syntax to set user-defined properties that can also be called collections.

Application ("Property/collection name") = value

We can use the following script to declare and establish the properties of the Application object.

<%
Application ("MyVar") = "Hello"
Set application ("MyObj") = Server.CreateObject ("MyComponent")
%>

Once we have assigned the properties of the Application object, it will persist until the WEB Server service is shut down so that application stops. Because the values stored in the Application object can be read by all users of the application, the properties of the Application object are particularly useful for passing information between users of the application.

   second, the method

The Application object has two methods that are used to handle the issue of multiple users writing to data stored in application

1. The Lock method prevents other clients from modifying the properties of the Application object.

The Lock method prevents other customers from modifying the variables stored in the Application object to ensure that only one customer can modify and access the application variable at the same time. If the user does not explicitly call the Unlock method, the server will unlock the Application object after the. asp file ends or times out.

Let's take a look at the following program that uses application to record the number of page visits:

<%
Dim numvisitsnumvisits=0
Application.lockapplication ("numvisits") = Application ("NumVisits") + 1
Application.UnLock
%>

Welcome to this page, you are the <%= application ("NumVisits")%> visitor of this page!

Save the above script in your. asp file, it is easy to add a counter to your page.

2. In contrast to the Lock method, the Unlock method allows other clients to modify the properties of the Application object.

In the above example, in the above example, the Unlock method unlocks the object, allowing the next client to increase the value of numvisits.

  Iii. Events

1, Application_OnStart

The Application_OnStart event occurs before the first creation of a new session (that is, the Session_OnStart event). The Application_OnStart event is triggered when the WEB server starts and allows requests for files contained by the application. The process of Application_OnStart events must be written in the Global.asa file.

The syntax for the Application_OnStart event is as follows:

< SCRIPT language=scriptlanguage runat=server>
Sub Application_OnStart ...
End Sub
</script>

2, Application_OnEnd

The Application_OnEnd event occurs after the application exits after the Session_OnEnd event, and the processing of the Application_OnEnd event must also be written in the Global.asa file.

Let's take a look at some of the things you should be aware of when using application objects. {Shanghai Cheng Kai Male Hospital: http://www.edzl.cn/editing and finishing}


ASP built-in objects cannot be stored in the Application object. For example, each of the following lines returns an error.

<%
Set application ("Var1") =session
Set application ("Var2") =request
Set application ("Var3") =response
Set application ("VAR4") =server
Set application ("VAR5") =application
Set application ("VAR6") =objectcontext
%>

If you store an array in an Application object, do not directly change the elements stored in the array. For example, the following script cannot be run.

<% Application ("StoredArray") (3) = "New Value"%>

This is because the Application object is implemented as a collection. The array element StoredArray (3) did not get a new assignment. This value will be included in the Application object collection and will overwrite any information previously stored in this location. It is recommended that you get a copy of the array before retrieving or altering the objects in the array when storing the array in the Application object. In an array operation, you should then store all the arrays in the Application object so that any changes you make will be stored. This is demonstrated by the following script.

---asp8a.asp---

<%
Dim MyArray ()
Redim MyArray (5)
MyArray (0) = "Hello"
MyArray (1) = "Some other string"
Application.Lock
Application ("StoredArray") =myarray
Application.UnLock
Response.Redirect "Asp8b.asp"
%>

---asp8b.asp---

<%
Localarray=application ("StoredArray")
LocalArray (1) = "There"
Response.Write LocalArray (0) &localarray (1)
Application.Lock
Application ("StoredArray") =localarray
Application.UnLock
%>

Another very useful ASP built-in object that has a similar effect to the Application object is the Session. We can use the session object to store the information needed for a particular user session. When a user jumps between pages of an application, the variables stored in the Session object are not purged, and the variables are always present when the user accesses the page in the application. When a user requests a Web page from an application, if the user does not yet have a session, the Web server automatically creates a Session object. When the session expires or is discarded, the server terminates the session.

The session object on the server can be managed by sending a unique Cookie to the client program. When a user requests a page in an ASP application for the first time, the ASP checks the HTTP header information to see if there is a Cookie named ASPSessionID in the message, and if so, the server initiates a new session and generates a globally unique value for the session. In sending this value to the client as the value of the new ASPSessionID cookie, this cookie is used to access information stored on the server that belongs to the client program. The most common function of a Session object is to store the user's preferences. For example, if a user indicates that they do not like to view a graphic, they can store the information in the Session object. In addition, it is often used in the process of identifying client identities. It is important to note that session state is reserved only in browser that supports cookies, and if the customer turns off the cookie option the session will not work.

   First, the attribute

1, SessionID

The SessionID property returns the user's session ID. When you create a session, the server generates a separate identity for each session. The session ID is returned as a long shaping data type. In many cases SessionID can be used for WEB page registration statistics.

2. TimeOut

The Timeout property specifies the time-out period for the Session object for the application, in minutes. If a user does not refresh or request a Web page within that time-out period, the session terminates.

   second, the method

The session object has only one method, that is, the Abandon,abandon method deletes all objects stored in the session object and frees the source of those objects. If you do not explicitly call the Abandon method, the server will delete these objects once the session times out. The following example frees session state when the server finishes processing the current page.

<% Session.Abandon%>

   Iii. Events

The session object has two events that can be used to start and release the Session object is the run process.

1. The Session_OnStart event occurs when a new session is created on the server. The server processes the requested page before executing the script. The Session_OnStart event is the best time to set session-time variables because they are set before any page is accessed.

Although the Session object remains while the Session_OnStart event contains the Redirect or End method call, the server stops processing the Global.asa file and triggers the script in the file that Session_OnStart the event.

To ensure that a user always starts a session when a particular Web page is opened, the Redirect method can be called in the Session_OnStart event. When the user enters the application, the server creates a session for the user and processes the Session_OnStart event script. You can include the script in the event to check whether a user-opened page is the startup page, or, if not, instruct the user to invoke the Response.Redirect method to start the Web page. The procedure is as follows:

< SCRIPT Runat=server language=vbscript>
Sub Session_OnStart
StartPage = "/myapp/starthere.asp"
CurrentPage = Request.ServerVariables ("Script_name")
If StrComp (currentpage,startpage,1) Then
Response.Redirect (StartPage)
End If
End Sub
</script>

The above program can only be run in a browser that supports cookies. Because browsers that do not support cookies cannot return SessionID cookies, the server creates a new session whenever a user requests a Web page. This way, for each request server, the Session_OnStart script is processed and the user is redirected to the startup page.

2. The Session_OnEnd event occurs when the session is discarded or timed out.

Things to note about using Session Objects application objects are similar, please refer to the previous article.

Sessions can be started in the following three ways:

1. A new user requests access to a URL that identifies an. asp file in an application, and the application's Global.asa file contains the Session_OnStart process.

2. The user stores a value in the Session object.

3. The user has requested an. asp file for an application, and the application's Global.asa file uses the < object> tag to create an instance of the object with the session scope.

If a user does not request or refresh any pages in the application within a specified time, the session will automatically end. The default value for this period is 20 minutes. You can change the application's default time-out limit setting by setting the Session Timeout property in the Application Options property page in Internet Services Manager. This value should be set according to the requirements of your WEB application and the memory space of the server. For example, if you want users who browse your WEB application to stay on each page for only a few minutes, the session's default time-out value should be shortened. An overly long session timeout value will cause too many open sessions to drain your server's memory resources. For a specific session, you can set the Timeout property of the session object if you want to set a timeout value that is less than the default time-out. For example, the following script sets the time-out value to 5 minutes.

<% Session.Timeout = 5%>

Of course you can also set a timeout value that is greater than the default setting, and the Session.Timeout property determines the time-out. You can also explicitly end a session by using the Abandon method of the Session object. For example, provide an Exit button in the table to set the ACTION parameter of the button to the URL of an. asp file that contains the following commands.

<% Session.Abandon%>

Today, we have studied two in Web pages, especially web-based BBS or Chat often use ASP built-in objects, because these two objects in practical use is very practical, so in the next article, the author will use this we have learned the 4 ASP built-in objects, to show you a complete ASP Application, I believe that through this exercise, you can greatly deepen your understanding and mastery of ASP applications. Stay tuned for the dynamic website Design terrorize--asp Chapter (9).

ASP Basics Tutorial: ASP built-in object application and session

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.