Session StaticObjects collection
The StaticObjects collection contains all objects created with the <OBJECT> tag in the Session object scope. This collection can be used to determine the value of a specific property of an object, or to iterate over the collection and obtain all properties for all objects.
grammar
Session.StaticObjects(Key)
parameters
Key
The attribute to retrieve.
Note: Use a loop control structure to iterate over keywords in the StaticObjects collection. See the example below.
<%
Dim objprop
For Each objprop in Session.StaticObjects
Response.write(objproperty & " : " & Session.StaticObjects(objprop) & "<BR>")
Next
%>
Session Contents Collection
The Session.contents collection includes all items created for this session without using the <OBJECT> tag. This collection can be used to determine the value of a specified session item or to iterate through the collection and retrieve a list of all items in the session.
grammar
Session.Contents(Key)
parameter
Key
The name of the property to get.
Note You can use a loop control structure to loop through keywords in the Contents collection. The following example demonstrates this process.
<%
Dim sessitem
For Each session in Session.Contents
Response.write(sessitem & " : " & Session.Contents(sessitem) & "<BR>")
Next
%>
CodePage
The CodePage property determines the code page that will be used to display dynamic content.
grammar
Session.CodePage(=Codepage)
parameter
Codepage
This is an unsigned integer that represents the valid code page of the system on which the ASP scripting engine is running.
Annotation code page is a character set that can include numbers, punctuation marks, and other letters. Different code pages can be used for different languages and regions. For example, ANSI code page 1252 is used for US English and most European languages; OEM code page 932 is used for Japanese Kanji.
A code page is a character table that maps characters into single-byte or multi-byte values. Many code pages share the characters between 0x00 - 0x7F in the ASCII character set.
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.
grammar
Session.Abandon
annotation
When the Abandon method is called, the current Session object will be deleted in order, but the object will not be actually deleted until all script commands in the current page are processed. This means that when Abandon is called, variables stored in the Session object can be accessed on the current page, but not on subsequent Web pages.
For example, in the following script, the third line prints the Mary value. This is because the Session object is not deleted before the server finishes processing the script.
<%
Session.Abandon
Session("MyName") = "Mary"
Response.Write(Session("MyName"))
%>
If you access the MyName variable on subsequent Web pages, you will find that it is empty. This is because when the page containing the previous example finished processing, MyName was deleted along with the previous Session object.
When the session is abandoned and subsequent Web pages are opened, the server creates a new Session object. You can store variables and objects in the new Session object.
Example The following example releases session state when the server has finished processing the current page.
<% Session.Abandon %>
LCID
The LCID attribute determines the location identifier used to display dynamic content.
grammar
Session.LCID(=LCID)
parameter
LCID
Effective site identification.
Comment
LCID specifies a site identifier, which is an international standard abbreviation that uniquely identifies a site defined by a certain system.
SessionID
The SessionID property returns the user's session ID. When creating a session, the server generates a separate ID for each session. The session ID is returned as a long data type.
grammar
Session.SessionID
Note Do not use the SessionID property to create primary keys for database applications. This is because if the Web server is restarted, some of the SessionID values may be the same as they were before the server was terminated. You can use an auto-incrementing column data type instead, such as IDENTITY in Microsoft® SQL Server, or COUNTER in Microsoft® Access.
Session_OnStart
The Session_OnStart event occurs when the server creates a new session. The server processes the script before executing the requested page. The Session_OnStart event is the best time to set session variables because they are set before any page is accessed. All built-in objects (Application, ObjectContext, Request, Response, Server and Session) can be used and referenced in the Session_OnStart event script.
grammar
<SCRIPT LANGUAGE=ScriptLanguage RUNAT=Server> Sub Session_OnStart. . .End Sub 'by aspxuexi.com
</SCRIPT>
Parameters
ScriptLanguage
Specifies the scripting language used to script the event. Can be any language that supports scripting, such as VBScript or JScript. If you have multiple events using the same scripting language, you can organize them under a set of <SCRIPT> tags.
Example Although the Session object will persist if the Session_OnStart event contains a Redirect or End method call, the server will stop processing the Global.asa file and trigger the script in the file that triggered the Session_OnStart event.
For example, in order to ensure that the user always starts a session when opening a specific Web page, you can call the Redirect method in the Session_OnStart event. When the user enters the application, the server creates a session for the user and handles the Session_OnStart event script. You can include script in this event to check whether the page the user opens is a startup page, and if not, instruct the user to call the Response.Redirect method to launch the page. Its demonstration is shown in the following example.
<SCRIPT RUNAT=Server Language=VBScript>
SubSession_OnStart
' Make sure that new users start on the correct
' page of the ASP application. 'by aspxuexi.com
' Replace the value given to startPage below
' with the virtual path to your application's
' start page.
startPage = "/MyApp/StartHere.asp"
currentPage = Request.ServerVariables("SCRIPT_NAME")
' Do a case-insensitive compare, and if they
' don't match, send the user to the start page.
if strcomp(currentPage,startPage,1) then Response.Redirect(startPage) end ifEnd Sub</SCRIPT>
The above example will only work in browsers that support cookies. Because browsers that do not support cookies cannot return the SessionID cookie, the server creates a new session each time the user requests a Web page. This way, for every request, the server processes the Session_OnStart script and redirects the user to the startup page. If you are using the script below, it is recommended that you place a notice on the startup page telling users that the site requires a cookie-enabled browser.
Note: Please note that any Session_OnStart event script after the Redirect method will not be executed. Therefore, the Redirect method should be called at the end of your event script. Its demonstration is shown in the following example.
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SubSession_OnStart
' Session initialization script
'by aspxuexi.com
Response.Redirect "http:/server/app/StartHere.asp"
End sub
</SCRIPT>
In the example above, the Redirect method hides all text displayed to the client during execution of the session initialization script.
Session_OnEnd
The Session_OnEnd event occurs when a session is abandoned or times out. Among the server built-in objects, only Application, Server and Session objects are available.
grammar
<SCRIPT LANGUAGE=ScriptLanguage RUNAT=Server> Sub Session_OnEnd. . . End Sub
</SCRIPT>
Parameters
ScriptLanguage
Specifies the scripting language used to script the event. Can be any language that supports scripting, such as VBScript or JScript. If you have multiple events using the same scripting language, you can organize them under a set of <SCRIPT> tags.
Note: The MapPath method cannot be called in a Session_OnEnd script.
Timeout
The Timeout property specifies the timeout period in minutes for the application's Session object. If the user does not refresh or request the page within this timeout, the session will be terminated.
grammar
Session.Timeout[=nMinutes]
parameter
nMinutes
Specifies the number of minutes after which the session will be idle for the server to automatically terminate the session. The default value is 15 minutes.