In the previous article, the author gave you a detailed introduction of 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 objects, 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 make up the ASP application. Instead of using the Application object, we can share information between all users of a given application and persist the data during the server's run. Also, the Application object has control over how the application-tier data is accessed and the events that can be used to trigger the process when the application starts and stops.
Now let's learn application objects together.
Properties Although a Pplication object has no built-in properties, we can use the following syntax to set a user-defined property, also known as a collection.
A pplication ("Attribute/collection name") = value
We can declare and establish the properties of the Application object using the following script.
<%
Application ("MyVar") = "Hello"
Set application ("myobj") = Server.CreateObject ("MyComponent")
%>
Once we assign the properties of a Pplication object, it will persist until the W-EB Server service is turned off so that A pplication stops. Because the values stored in a Pplication object can be read by all users of the application, the properties of a Pplication object are particularly suitable for passing information between users of the application.
second, the method The Application object has two methods, all of which are used to work with multiple users writing to the data stored in application
1. The L Ock method prohibits other customers from modifying the properties of a Pplication object.
The Lock method prevents other customers from modifying 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 finishes or times out.
Let's take a look at the following program that uses a pplication 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 first <%= application ("NumVisits")%> visitors!
By saving the above script in your. asp file, you can easily add a counter to your page.
2, in contrast to the Lock method, the Unlock method allows other customers to modify the properties of the Application object.
In the above example, the Unlock method unlocks the object so that the next client can increase the value of the numvisits.
III. Events
1, Application_OnStart
A Pplication_onstart event occurs before the first time a new session (that is, the S Ession_onstart event) is created. A Pplication_onstart event is triggered when the W EB server is started and allows requests for files contained in the application. The process of a Pplication_onstart event must be written in the G Lobal.asa file.
The syntax for the Application_OnStart event is as follows:
< SCRIPT language=scriptlanguage runat=server>
Sub Application_OnStart ...
End Sub
</script>
2. The Application_OnEnd Application_OnEnd event occurs after the application exits at the Session_OnEnd event, and the process of Application_OnEnd event must also be written in the Global.asa file.
Let's take a look at some of the things you need to be aware of when using application objects.
You cannot store the A-SP built-in object in a Pplication 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 a 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 a Pplication object is implemented as a set. The array element S Toredarray (3) did not get a new assignment. This value will be included in the A Pplication object collection and will overwrite any information previously stored at this location. It is recommended that you get a copy of an array before retrieving or altering an object in an array when you store the array in a Pplication object. In an array operation, you should store all of the arrays in a Pplication 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 object that has a similar effect to the Application object is the session. We can use the session object to store the information that is required for a particular user conversation. When a user jumps between pages of an application, the variables stored in the Session object are not purged, and the variables always exist when the user accesses the page in the application. When a user requests a Web page from an application, the Web server automatically creates a Session object if the user does not have a conversation. When a session expires or is discarded, the server terminates the session.
You can manage the session object on the server 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 a Cookie named ASPSessionID in the message is sent, and if so, the server starts a new session and generates a globally unique value for the session. This value is sent to the client as the value of the new ASPSessionID cookie, which is used to access information that is stored on the server that is part of the client program. The most common function of the session object is to store user preferences. For example, if a user indicates that they don't like to view a graphic, they can store that information in the session object. In addition, it is often used in the process of identifying customer identities. Note that session state is reserved only in browsers that support cookies, and sessions will not work if the customer turns off the cookie option.
One, property
1, SessionID
The SessionID property returns the user's session identity. When you create a session, the server generates a separate identity for each session. The session identity is returned with a long shaping data type. In many cases SessionID can be used for WEB page registration statistics.
2. The TimeOut TimeOut property specifies the time-out period for the session object of the application in minutes. If the user does not refresh or request a Web page within the 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 releases the source of those objects. If you do not explicitly call the Abandon method, the server deletes these objects once the session times out. The following example releases 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 when it is running.
1, the Session_OnStart event occurs when a new session is created by the server. The server processes the requested page before it is executed. The Session_OnStart event is the best time to set session variables, because they are set before any pages are accessed.
Although the session object persists when the Session_OnStart event contains a Redirect or End method call, the server stops processing the Global.asa file and triggers the script in the file of the Session_OnStart event.
To ensure that a user always initiates a session when opening a particular W-EB page, the R Edirect method can be invoked in the S Ession_onstart event. When the user enters the application, the server creates a session for the user and processes the S Ession_onstart event script. You can include the script in this event to check that the user opens a page that is not a startup page, and if not, instruct the user to call R esponse. The Redirect method launches 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 programs can only run in browsers that support cookies. Because browsers that do not support cookies cannot return the SessionID cookie, the server creates a new session whenever the user requests a Web page. In this way, the Session_OnStart script is processed for each request server and the user is redirected to the startup page.
2. The Session_OnEnd event was discarded or timed out during the session.
Things to note about using Session Objects application objects are similar, please refer to the preceding text.
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 requests an application's. asp file, 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 page in the application within a specified time, the session ends automatically. The default value for this period is 2 0 minutes. You can change the default time-out limit setting for an application by setting the session Timeout property in the I nternet Service Manager for the Application Options property page. This value should be set according to the requirements of your W EB application and the memory space of the server. For example, if you want users of your W-EB application to stay only a few minutes per page, you should shorten the default timeout for the session. An overly long session timeout value will cause an open session to run out of memory resources for your server. For a specific session, you can set the T-imeout property of the S Ession object if you want to set a time-out value that is less than the default time-out. For example, the following script sets the timeout value to 5 minutes.
<% Session.Timeout = 5%>
Of course, you can also set a time-out value that is greater than the default setting, and the Session.Timeout property determines the timeout. You can also explicitly end a session by using the Abandon method of the Sessions object. For example, an Exit button is provided in the table, and the ACTION parameter of the button is set to the URL of the. asp file that contains the following commands.
<% Session.Abandon%>
Today, we've learned two of the most frequently used ASP-built objects on Web pages, especially web-based BBS or Chat, as these two objects in practical use, so the next article in 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, can greatly deepen your understanding of ASP applications and mastery. Please pay attention to "dynamic website design Skills--asp article (9)".