A specific explanation of the session's usage

Source: Internet
Author: User
Session|session

Session Object
You can use the session object to store the information that is required for a specific user conversation. This way, when a user jumps between the application's Web pages, the variables stored in the session object are not lost, but are persisted throughout the user's sessions.

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.

One of the most common uses of Session objects 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. For more information about using the session object, see "Managing Sessions" in the ASP applications section.

Note Session state is reserved only in browsers that support cookies.

Grammar
Session.collection|property|method

Collection
Contents contains items that have been added to the session with script commands.
StaticObjects contains objects created by <OBJECT> tag and given session scope.


Property
CodePage The code page that will be used for symbolic mapping.
LCID field identification.
SessionID returns the user's session validation.
Timeout the time-out period for application session state, in minutes.


Method
Abandon This method destroys the session object and frees its resources.


Event
A script that declares the following events in the Global.asa file.

Session_OnEnd
Session_OnStart


For more information about the above events and global.asa files, see the Global.asa reference.

Comments
You can store values in the Session object. The information stored in the session object is valid for both sessions and session scopes. The following script shows how two types of variables are stored.

<%
Session ("username") = "Janine"
Session (' age ') = 24
%>

However, if you store objects in the Session object, and you use VBScript as the primary scripting language. You must use the keyword Set. As shown in the following script.

<% Set session ("Obj1") = Server.CreateObject ("Mycomponent.class1")%>

You can then invoke the methods and properties that Mycomponent.class1 reveals on a later Web page, which is invoked as follows:

<% session ("Obj1"). MyMethod%>

You can also call by expanding the local copy of the object and using the following script:

<%
Set MyLocalObj1 = Session ("Obj1")
Mylocalobj1.myobjmethod
%>

Another way to create an object with session scope is to use the <OBJECT> tag in the Global.asa file.

However, built-in objects cannot be stored in the session object. For example, each of the following rows will return an error.

<%
Set session ("var1") = Session
Set session ("var2") = Request
Set session ("VAR3") = Response
Set session ("VAR4") = Server
Set session ("VAR5") = Application
%>

Before you store an object in the Session object, you must understand which threading model it is using. Only objects that are marked "Both" can be stored in a session object that does not have a single threaded conversation locked. For more information, see "Choosing a threading Model" in "Creating ASP components."

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

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

This is because the session 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.

We strongly recommend that you get a copy of an array before retrieving or altering an object in the array when you store the array in the Session object. In an array operation, you should store all of the arrays in the session object, so that any changes you make will be stored. This is demonstrated by the following script.

---file1.asp---
<%
' Creating and initializing the array
Dim MyArray ()
Redim MyArray (5)
MyArray (0) = "Hello"
MyArray (1) = "Some other string"

' Storing the ' array in the Session object
Session ("StoredArray") = MyArray

Response.Redirect ("file2.asp")
%>

---file2.asp---
<%
' Retrieving the ' array from the session Object
' and modifying its second element
LocalArray = Session ("StoredArray")
LocalArray (1) = "There"

' Printing out the string ' Hello there '
Response.Write (LocalArray (0) &localarray (1))

' Re-storing the ' array in the Session object
' This overwrites the values in StoredArray with the new values
Session ("StoredArray") = LocalArray
%>

Example
The following code assigns a string myname to a session variable named name, and assigns a value to the session variable named year, and is some. An instance of the OBJ component specifies a variable named myobj.

Session ("name") = "MyName"
Session ("Year") = 96
Set session ("myobj") = Server.CreateObject ("Someobj")
%>




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.