Detailed description of ASP built-in object application
In addition to the objects used to send, receive, and process data, ASP's built-in objects also have some very practical applications that represent the active server.ProgramAnd the object of a single user information.
Let's take a look at the application object. All. asp files in the same virtual directory and Its subdirectories constitute ASP applications. Instead of using the application object, we can share information among all users of a given application and persistently save data during server running. In addition, the application object also provides methods to control access to application layer data and events that can be used to trigger the process when the application starts or stops.
Next let's learn the Application Object together.
I. Attributes
Although the application object does not have built-in attributes, we can use the following syntax to set user-defined attributes, also known as collections.
Application ("attribute/Set Name") = Value
We can use the following script to declare and create the attributes of the Application object.
<%
Application ("myvar") = "hello"
Set Application ("myobj") = server. Createobject ("mycomponent ")
%>
Once an application object is assigned a property, it will persist until the Web Server service is disabled to stop the application. Because the values stored in the Application object can be read by all users of the application, the attributes of the Application object are especially suitable for transmitting information between users of the application.
Ii. Method
The application object has two methods, both of which are used to process data written by multiple users in the application.
1. The lock method prohibits other customers 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 variables at the same time. If you do 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 page access times:
<%
Dim numvisitsnumvisits = 0
Application. lockapplication ("numvisits") = Application ("numvisits") + 1
Application. Unlock
%>
Welcome to this page. You are the <% = Application ("numvisits") %> guest on this page!
Save the above script in your. asp file, and easily add a counter to your page.
2. opposite to the lock method, the unlock method allows other customers to modify the attributes of the Application object.
In the preceding example, the unlock method unlocks the object so that the next client can increase the value of numvisits.
Iii. Events
1. application_onstart
The application_onstart event occurs before the first session (session_onstart event) is created. The application_onstart event is triggered when the web server starts and allows requests to files contained in the application. The application_onstart event processing process must be written in the global. Asa file.
The syntax of 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 session_onend event when the application exits. The process of processing the application_onend event must also be written in the global. Asa file.
Next let's take a look at some precautions when using the application object.
ASP built-in objects cannot be stored in application objects. For example, an error is returned for each row below.
<%
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 scripts cannot be run.
<% Application ("storedarray") (3) = "New Value" %>
This is because the application object is implemented as a set. The array element storedarray (3) is not assigned a new value. This value will be included in the application object set and will overwrite any information stored before this location. We recommend that you retrieve a copy of the array before retrieving or modifying the objects in the array when storing the array in the Application object. When operating on the array, you should store all the arrays in the Application object, so that any changes you make will be stored. The following script demonstrates this.
--- 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 practical ASP built-in object that is similar to the application object is session. We can use the session object to store the information required for a specific user session. When a user jumps between pages of an application, the variables stored in the session object are not cleared, and the variables always exist when the user accesses the page in the application. When a user requests a web page from an application, if the user does not have a session, the web server automatically creates a session object. When a session expires or is abandoned, the server terminates the session.
You can manage session objects 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, ASP checks the HTTP header information to check whether a cookie named aspsessionid is sent in the message. If yes, the server starts a new session and generates a globally unique value for the session. This cookie is used when the value is sent to the client as the value of the new aspsessionid cookie, you can access the information of the customer program stored on the server. Session objects are most commonly used to store user preferences. For example, if you specify that you do not like to view images, you can store the information in the session object. In addition, it is often used in the process of identifying customer identities. Note that the session status is retained only in the browser that supports cookies. If the cookie option is disabled, the session cannot be used.
I. Attributes
1. sessionid
The sessionid attribute returns the user's session ID. When creating a session, the server generates a separate identifier for each session. The session ID is returned as a long integer. In many cases, sessionid can be used for web page registration statistics.
2. Timeout
The timeout attribute specifies the timeout period for the session object of the application in minutes. If the user does not refresh or request the webpage within the timeout period, the session will be terminated.
Ii. Method
The Session object has only one method, namely, abandon. The abandon method deletes all objects stored in the session object and releases the source of these objects. If you do not explicitly call the abandon method, the server will delete these objects once the session times out. When the server finishes processing the current page, the following example releases the session status.
<% Session. Abandon %>
Iii. Events
The Session object has two events that can be used to start and release a session object.
1. The session_onstart event occurs when the server creates a new session. The server processes the script before executing the request page. Session_onstart events are the best time to set session-period variables, because they are set before accessing any page.
Although the session object remains unchanged when the session_onstart event contains a redirect or end method call, the server stops the script in the file that processes the global. Asa file and triggers the session_onstart event.
To ensure that a session is always started when you open a specific web page, you can call the Redirect method in the session_onstart event. When a 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 this event to check whether the page opened by the user is a startup page. If not, it instructs the user to call the response. Redirect method to start the webpage. 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 preceding program can only 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. In this way, each request server processes the session_onstart script and redirects the user to the startup page.
2. The session_onend event occurs when the session is abandoned or timed out.
For considerations about using session objects, see the previous article.
Sessions can be started in the following three ways:
1. A new user requests to access a URL that identifies the. asp file in an application and the Global. Asa file of the application 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 global. Asa file of the application creates an instance with a session scope object using the <Object> tag.
If the user does not request or refresh any page in the application within the specified time, the session ends automatically. The default value is 20 minutes. You can change the default timeout limit of an application by setting the Session Timeout attribute on the "application options" property page in Internet Service Manager. This value should be set based on the requirements of your web application and the memory space of the server. For example, if you want users browsing your web application to stay on each page for only a few minutes, you should shorten the default session timeout value. A too long session timeout value will cause too many sessions to be opened and exhaust your server's memory resources. For a specific session, if you want to set a timeout value smaller than the default timeout value, you can set the timeout attribute of the session object. For example, the following script sets the timeout value to 5 minutes.
<% Session. Timeout = 5%>
Of course, you can also set a timeout value greater than the default value. The session. Timeout attribute determines the timeout value. You can also use the abandon method of the session object to explicitly end a session. For example, a "exit" button is provided in the table to set the action parameter of the button to the URL of the. asp file containing the following commands.
<% Session. Abandon %>