In the previous author, I will introduce you to the use of ASP built-in object response, in which the author will continue to introduce you to the other two very practical and important ASP built-in object Application and Session. In addition to the objects used to send, receive, and process data in the built-in object of the ASP, there are also some objects that represent the Active Server application and a single user information.
Let's take a look at the Application object. All .asp files in the same virtual directory and its subdirectory constitute an ASP application. We don't use the Application object, share information between all users of a given application, and save data during the server run. Moreover, the Application object also has an event that controls access to application layer data and an event that can be used to trigger the process when the application starts and stops.
Let us learn the Application object together.
First, attribute
Although the Application object does not have built-in attributes, we can use the following syntax to set the user-defined attributes.
Application ("Properties / Collection Name") = value
We can use the following scripts to declare and create the properties of the Application object.
<%
Application ("MyVar") = "Hello"
Set Application ("MyObj") = Server.createObject ("MyComponent")
%>
Once we assign the properties of the Application object, it will last forever until the Web Server service is turned off to stop. Since the value stored in the Application object can be read by all users of the application, the properties of the Application object are particularly suitable for transmitting information between applications.
Second, the method
The Application object has two methods that are all useful to handle multiple users to write data stored in Application.
1, the LOCK method prohibits other customers to modify the properties of the Application 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 Application variables at the same time. If the user does not explicitly call the UNLock method, the server will be locked to the Application object after the end or timeout of the .asp file.
Let's take a look at the program that uses Application to record the number of page access:
<%
DIM NumvisitsNumVisits = 0
Application.lockApplication ("Numvisits") = Application ("Numvisits") 1
Application.unlock
%>
Welcome to this page, you are the "Numvisits")%> A visitor!
Save the above scripts in your .asp file, easily add a counter to your page.
2, and the LOCK method, the UNLOCK method allows other customers to modify the properties of the Application object.
In the above example, in the above example, the UNLOCK method relieves the lock of the object such that the next client can increase the value of NumVisits.
Third, an event
1, Application_OnStart
The Application_onstart event occurs before the first creation of a new session (ie, the session_onstart event). The Application_onstart event is triggered when the web server starts and allows the files included in the application. The process of Application_onstart event must be written in the global.asa file. The syntax of the Application_onstart event is as follows:
Sub Application_onstart..
End Sub
Script>
2, Application_ONEND
Application_onend event occurs after the application exits, and the process of the Application_ONEND event must also be written in the global.asa file.
Let's take a look at some of the matter you must pay attention to when using the Application object.
The ASP built-in object cannot be stored in the Application object. For example, each line below 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 the Application object, don't change the elements stored in an array directly. For example, the following scripts cannot be run.
<% Application ("StoredArray") (3) = "New value"%>
This is because the Application object is implemented as a collection. Array element StoredArray (3) does not get a new assignment. And this value will be included in the Application object collection and will overwrite any information previously stored before. It is recommended that when you store an array in an Application object, a copy of the array is obtained before retrieving or changing the objects in the array. When operating the array, you should store all the arrays in the Application object so that any changes you have made will be stored. The following scripts are demonstrated.
--- 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 with similar effects with Application objects is Session. We can use the Session object to store the information required for a particular user session. When the user jumps between the pages of the application, the variables stored in the session object will not be cleared, and these variables always exist when the user accesses the page in the application. When the user requests a web page from the application, if the user has no session, the web server will automatically create a session object. When the session expires or abandoned, the server will terminate the session. Send a unique cookie to the client program to manage the session object on the server. When the user first requested a page in the ASP application, the ASP wants to check the HTTP header information to see if there is a cookie named aspsessionID in the message. If there is, the server will start a new session, and Generate a globally unique value for the session, which is sent to the client as the value of the new ASPSessionID cookie, is using this cookie, which can access the information stored on the server belonging to the client. The most common role of the Session object is to store the user's preferences. For example, if the user indicates that you don't like the viewing graph, you can store this information in the Session object. In addition, it is often used in a program that identifies the customer identity. It should be noted that the session status is only reserved in a browser that supports the cookie. If the customer turns off the cookie option, the session does not work.
First, attribute
1, sessionid
The sessionID property returns the user's session ID. When you create a session, the server generates a separate identifier for each session. The session identifier returns in a long plastic data type. In many cases, SessionID can be used for web page registration statistics.
2, Timeout
The Timeout property specifies the timeout time limit for the SESSION object for the application in minutes. If the user does not refresh or request a web page within the timeout time limit, the session will terminate.
Second, the method
The Session object has only one method, that is, Abandon, the Abandon method deletes all the objects stored in the session object and releases these objects. If you are not clearly calling the Abandon method, once the session is timeout, the server will delete these objects. The following example will release the session status when the server is handled in the current page.
<% Session.abandon%>
Third, an event
The Session object has two events available to start and release in Session objects.
1. The session_onstart event occurs when the server creates a new session. The server handles the script before performing the requested page. The session_onstart event is the best time to set the session period, as you will set them before accessing any pages.
Although the Session object remains in the case where the session_onstart event is called, the session object will remain, but the server will stop processing the global.asa file and triggered the script in the file of the session_onstart event.
To ensure that users always start a session while opening a particular web page, you can call the Redirect method in the session_onstart event. When a user enters an application, the server will create a session for the user and process the session_onstart event script. You can include the script in this event to check the user's open page, if not, indicate that the user calls the response.redirect method to start the web. The procedure is as follows:
SUB session_onstartstartpage = "/myapp/starthere.asp"
CurrentPage = Request.SerVariables ("script_name")
IF strcomp (CurrentPage, StartPage, 1) THEN
Response.Redirect (StartPage)
END IF
End Sub
Script>
The above procedure can only be run in a browser that supports a cookie. Since the browser that does not support the cookie cannot return SessionID cookies, whenever the user requests the web page, the server creates a new session. Thus, the session_onstart script will be processed for each request server and redirect the user to the startup page.
2. The session_onend event occurs during the session or timeout.
For the Application objects that you need to pay attention to using the session object, please refer to the forebel.
The session can be started in three ways:
1. A new user requests to access a URL that identifies the .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
If the user does not request or refresh any page in the application within the specified time, the session will automatically end. The default value for this time is 20 minutes. You can change the default timeout limit setting for the application by setting the "Session Time" property in the "Application Options" property page in the Internet Service Manager. This value should be set according to your web application requirements and server memory space. For example, if you want to browse your web application users stay only for a few minutes in each page, you should shorten the default timeout values for the session. Excessive session timeout will result in too many of the open sessions and exhausted your server's memory resources. For a specific session, if you want to set a timeout value less than the default timeout value, you can set the timeout property 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 up a timeout value greater than the default setting, and the session.Timeout property determines the timeout value. You can also explicitly end a session through the ABandon method of the Session object. For example, a "exit" button is provided in the table to set the Action parameters of the button to the URL of the .asp file containing the following commands.
<% Session.abandon%>
Today, we have learned two in the web page, especially web-based BBS or CHATs, often use ASP built-in objects, because these two objects are very practical in practical use, so the author will apply to this. After 4 ASP built-in objects, give you a complete ASP application, believe that through this exercise, you can greatly deepen your understanding and master of the ASP application. Please pay attention to "Dynamic Website Design Eighth War --ASP (9)".