In ASP, there are two internal objects that can store some information. They are the Application object and the Session object. The Application object is for the entire application period and is shared by all users who visit the website. , and Session is for the duration of the session, it only exists for the current user.
Introduction to the Session Object
When you work with an application on your computer, you open it, make changes, and then close it, much like a conversation (Session). The computer knows who you are, and it knows when you open and close applications. However, problems arise on the Internet, because HTTP addresses cannot maintain status, and the web server does not know who you are and what you have done.
The main purpose of the Session object is to store some information for each user who visits the website. For example, when the user logs in, we can add it to the user's Session Store information to identify that the current user is logged in.
The principle of Session is this. When a user visits the website for the first time, IIS assigns an identifier to the user. This identifier is a long random string. , this random string is called SessionID, and then the server sends it to the client and saves it in Cookies. When the user visits other pages on the server, the server obtains the SessionID and obtains the SessionID from the memory. Relevant data is placed in the collection of Session objects.
Contents collection
We can store some information about the current user in this collection. For example, the following code shows how to store and read data:
<% '名字为username的Session集合中存储了一个“ZhangSan”字符串 Session.Contents("username") = "ZhangSan" Dim UserName '读取Session中的数据,可以省略Contents' UserName = Session.Contents("username")和下面一样 UserName = Session("username") Response.Write("<h2 id="nbsp-nbsp-UserName-nbsp-nbsp">" & UserName & "</h2>") %>
Session object There are three methods (Contents.Remove, Contents.RemoveAll, Abandon), which are used to delete the data in the Session collection or abandon the current Session.
First example(SessionContents.asp) We will demonstrate how to use the Remove and RemoveAll methods. The code is as follows:
...<h3 id="当前SessionID值为-nbsp-Session-SessionID">当前SessionID值为 <%=Session.SessionID%></h3><h3 id="Session中存储数据">Session中存储数据</h3><%'利用 Contents.Count 遍历 Session 的过程Sub Traversal_P() Dim i For i = 1 To Session.Contents.Count Response.Write("Session(""" & Session.Contents.key(i) & """) = " & Session.Contents(i)) Response.Write("<br>") NextEnd Sub'For Each 遍历 Session.Contents 集合 Sub Traversal_E() Dim x For Each x In Session.Contents Response.Write("Session(""" & x & """) = " & Session(x)) Response.Write("<br>") NextEnd Sub'Session.Contents中存储了多个数据,如下Session.Contents("username") = "ZhangSan"Session.Contents("password") = "12345678"Session.Contents("date")="2015/08/14"Session.contents("author")="pchmonster"'遍历 Contents 集合Traversal_E()%><hr><h3 id="删除名为username的数据">删除名为username的数据</h3><%'删除 username 数据Session.Contents.Remove("username")'重新遍历 Contents 集合Traversal_P()%><hr><h3 id="删除所有的Session数据">删除所有的Session数据</h3><%'删除所有的数据Session.Contents.RemoveAll() Traversal_E()%>...
The above code will be displayed as follows after running:
These codes demonstrate two methods of traversing the Session.Contents collection, please take a closer look.
The second example (SessionAbandon.asp) demonstrates the effect of the Abandon method. Through the demonstration, we can see that the difference between the RemoveAll method and the Abandon method is that RemoveAll only deletes the current collection. But the client still uses the same SessionID (the SessionID remains unchanged in the first example). After the Abandon method is called, the Session collection can still be accessed on the current page. After the page is closed or refreshed, the previous Session will be deleted (the SessionID will change in this example).
The code is as follows:
<%'Abandon的使用后,在当前页面仍可以访问Session集合,关闭页面或刷新后'会使Session被删除,SessionID也就会改变Session.Abandon()'首先我们要记录一下SessionID的值,存放到Cookies中Dim numVisits, SID Response.Cookies("numVisits").Expires = DateAdd("d", 10, Now) Response.Cookies("SID").Expires = DateAdd("d", 10, Now) SID = Request.Cookies("SID") numVisits = Request.Cookies("numVisits")If numVisits = "" or SID = "" Then '如果是第一次运行该页面,则记录当前Sessio nID值 Response.Cookies("numVisits") = 1 Response.Cookies("SID") = Session.SessionID%> <h3 id="您这是第一次访问该页面-当前页面的SessionID为">您这是第一次访问该页面,当前页面的SessionID为</h3> <h2><%=Session.SessionID%></h2><%Else%> <hr> <h3 id="您这是第-numVisits-次访问该页面-当前页面的SessioID为">您这是第<%=numVisits%>次访问该页面,当前页面的SessioID为</h3> <h2><%=Session.SessionID%></h2> <h3 id="您第一次访问时的SessionID为">您第一次访问时的SessionID为</h3> <h2><%=Request.Cookies("SID")%></h2><% numVisits = numVisits + 1 Response.Cookies("numVisits") = numVisitsEnd If%>
The first time you run this page, the current SessionID will be recorded in Cookies, as shown below:
After refreshing the page multiple times or reopening it, because of the Abandon method, the Session will be deleted and the SessionID will keep changing, as shown below:
CodePage, SessionID, Timeout attributes
CodePage attribute defines the character set of the output content of the current page. The character set here is represented by numbers. For example,
936 means Chinese Simplified (GB2312), Simplified Chinese
950 means Chinese Traditional (Big5), Traditional Chinese
65001 means Unicode (UTF-8)
Special instructions
Applies to all static strings
Response.CodePage, Session.CodePage applies to all dynamic output The scope of string
Response.CodePage is only in a single response
Session.CodePage is in the scope of all responses in a session
SessionID 属性可以获得当前用户的 SessionID,有时候在客户端浏览器不支持 Cookies 的情况下,你可以将 SessionID 附加在客户端的 QueryString 变量中,从而标识每一个客户端。
Timeout 属性用于设定客户的 Session 超时期。客户对于 SessionID 并不是长期占有的,在其一段时间内没有和服务器端进行任何交互后,服务器端将放弃该 Session。
下面的代码(SessionCST.asp)中将演示这个三个属性的使用方法,代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <%Session.CodePage = 65001'作用于所有动态输出的字符串%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>CodePage、SessionID、TimeOut属性的应用</title> </head> <body> <h3 id="当前页面使用的CodePage是">当前页面使用的CodePage是:</h3> <h2><%=Session.CodePage%></h2> <hr> <h3 id="当前页面的SessionID是">当前页面的SessionID是:</h3> <h2><%=Session.SessionID%></h2> <hr> <h3 id="当前页面Session默认超时时间为">当前页面Session默认超时时间为:</h3> <h2 id="Session-Timeout-分钟"><%=Session.Timeout%>分钟</h2> </body> </html>
运行后,效果如下:
【相关推荐】
1. ASP免费视频教程
The above is the detailed content of Introducing three methods of Session object in ASP. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

C#.NET is a popular choice for building microservices because of its strong ecosystem and rich support. 1) Create RESTfulAPI using ASP.NETCore to process order creation and query. 2) Use gRPC to achieve efficient communication between microservices, define and implement order services. 3) Simplify deployment and management through Docker containerized microservices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment