ASP Basics Tutorial: ASP Built-in Application and Session objects
0 comments
In the article the author introduces us to the ASP Response object to use built in this article the author continues to tell you two other very useful and important to the built-in ASP objects Application and Session.
Built in ASP objects in addition to send, receive and process data objects, there are some very practical applications on behalf of Active Server and individual user information object.
Let us first take a look at Application Object. In the same virtual directory and its subdirectories for all. Asp file form ASP applications. We not only can use the Application object, in a given application to share information between all users, and the server is running during the long-lasting data. Moreover, Application object also control access to methods and application layer data can be used to start and stop the application process when the trigger event.
Let us learn together the following Application object.
First, property
Although there is no built-in Application object property, but we can use the following syntax to set user-defined attributes can also be called a collection.
Application (“property / collection name”) = value
We can use the following script statement and the establishment of Application object.
<%
Application (“MyVar”) = “Hello”
Set Application (“MyObj”) = Server.CreateObject (“MyComponent”)
%>
Once we have assigned the Application object’s property, it will be sustained there until the closure of WEB Application Server service makes stops. Because the stored value in the Application object can be the application that all users read, so Application object properties particularly suitable for application transfer information between users.
Second, methods
Application object has two methods, which are used to handle multiple users on the data stored in the Application for written questions
1, Lock method changes against other client Application object.
Lock method prevents other clients modify stored in the Application object variable, to ensure that only one client at the same time to modify and access Application variables. If the user does not explicitly call the Unlock method, the server will be. Asp file ends or times out after the lifting of the Application object’s lock.
Let us look at this with Application to record the following page visits program:
<%
Dim NumVisitsNumVisits = 0
Application.LockApplication (“NumVisits”) = Application (“NumVisits”) + 1
Application.Unlock
%>
Welcome to our website, you are the first page <% = Application (“NumVisits”)%> visitors!
Save the above script in your. Asp file, on your page easy to add a counter.
2, and Lock methods contrary, Unlock method allows other clients modify the Application object.
In the above example, the above example, Unlock methods to lift the object lock, so the next client to increase NumVisits value.
Third, the event
1, Application_OnStart
Application_OnStart event for the first time to create a new session (ie Session_OnStart events) took place before. WEB server when the application starts and allows the file contains Application_OnStart event is triggered when the request. Application_OnStart handling procedures must be written in the Global.asa documents.
Application_OnStart event syntax is as follows:
<SCRIPT LANGUAGE = ScriptLanguage RUNAT = Server>
Sub Application_OnStart…
End Sub
</ SCRIPT>
2, Application_OnEnd
Application_OnEnd event in the application exits after the events took place in Session_OnEnd, Application_OnEnd of the case must also be written in Global.asa documents.
Let us look at the use of Application object is important to note some of the issues.
Can not be stored in the Application object ASP built-in objects. For example, following each line returned 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 are an array are stored in the Application object, do not directly change the elements stored in the array. For example, the following script does not run.
<% Application (“StoredArray”) (3) = “new value”%>
This is because the Application object is implemented as a collection. Array element StoredArray (3) did not obtain a new assignment. And this value will be included in the Application object collection and will cover the location of any previously stored information. Recommended that you store in the array when the Application object, retrieve or change in the object array before the array to obtain a copy. Operation on an array, you should then all stored in the Application object array, so that any changes you make will be stored. The following scripts demonstrate 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
%>
Application object has a similar role with another very useful ASP built-in object is the Session. We can use the Session object to store a particular user session information needed. When the user jumps between pages application, the Session object is stored in the variable does not clear, and users access the pages in your application, these variables remain. When the user requests a Web page from the application, if the user has no session, the Web server automatically creates a Session object. When the session is expired or abandoned, the server will terminate the conversation.
Through only sent to the client can manage the server Cookie Session object. When the user first ASP application requests a page, ASP HTTP header information to check to see if there are named in the packet sent over ASPSESSIONID’s Cookie, and if so, then the server will start a new session, and the session to generate a globally unique value, in this value as the value of the new ASPSESSIONID Cookie sent to the client, it is to use this Cookie, you can access stored on the server belonging to client information. Session object of the most common role is to store user preferences. For example, if users do not like to see specified in graphics, the information can be stored in the Session object. In addition it is also often used in identification of customer identity program. It should be noted that the session state only in support of the browser cookie to keep, if the customer closed the Cookie option, Session would be unable to play.
First, property
1, SessionID
SessionID property returns the user’s session ID. Create a session, the server for each session, a separate identity. Session ID to return a long data type plastic. In many cases, can be used in WEB page SessionID registration statistics.
2, TimeOut
Timeout property in minutes of the Session object for the application specified timeout period. If the user within the timeout period does not refresh or request page, then the session will terminate.
Second, methods
Session object is only one way out is Abandon, Abandon method to delete all stored in the Session object object and release the source of these objects. If you do not call the Abandon method explicitly, once the session timeout, the server will delete these objects. When the server is processing the current page, the following example will release the session state.
<% Session.Abandon%>
Third, the event
Session object has two events can be used in the Session object is to run up and release process.
1, Session_OnStart event occurs when the server creates a new session. Server page in the execution of the request before processing the script. Session_OnStart event is to set the session’s best time of variables, because any page in the visit will be the first to set them before.
Although Session_OnStart event Redirect or End method call that contains the case of Session object will remain, but the server will stop processing Session_OnStart Global.asa file and trigger events in the script file.
To ensure that users open a particular Web page always start a session, you can call the Redirect method Session_OnStart event. When the user enters the application, the server creates a session for the user and handle Session_OnStart event script. You can script included in the incident in order to check the user opens the page is not the start page, if not, call the Response.Redirect method to instruct the user start page. Procedures are 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 program can only support the cookie in the browser to run. Because the browser does not support cookie can not return SessionID cookie, so that whenever a user requests Web pages, the server will create a new session. Thus, for each request server will handle Session_OnStart script and redirect the user to start page.
2, Session_OnEnd event was abandoned in the session or timeout occurred.
On the use of Session object needs to pay attention to Application object is similar, please refer to the foregoing.
Session can be started by the following three ways:
1, a new user requests to access a URL, the URL identifies a particular application. Asp files and the application process Global.asa file contains Session_OnStart.
2, the user is stored in the Session object has a value.
3, user requests an application. Asp files and the application of the Global.asa file using the <OBJECT> tag to create an object with session scope instance.
If the user has not requested within the specified time, or refresh any page of the application, the session will automatically end. During this time, the default value is 20 minutes. Available through the Internet Services Manager to set the “Application options” property page in the “session timeout” attribute to change the application’s default time-out limit setting. Should be based on your Web application server’s memory space requirements and to set this value. For example, if you wish to browse your Web application users stay on each page just a few minutes, it should reduce the default session timeout value. Long session timeout value will lead to excessive depletion of the open session of your server’s memory resources. For a particular session, if you want to set a timeout value smaller than the default timeout value, you can set the Session object’s Timeout property. For example, following this script, set the timeout value to 5 minutes.
<% Session.Timeout = 5%>
Of course, you can also set a timeout value greater than the default setting, Session.Timeout property determines the timeout value. You can also Session objects Abandon method explicitly end a conversation. For example, in the form of a “Quit” button, the button of the ACTION parameter set to include the following commands. Asp file URL.
<% Session.Abandon%>
« ASP Basics Tutorial: ASP Built-in Object Request Next Post
ASP Tutorial: Global.asa file to use »

