Session 会话对象中的经典ASP存储对象
我不熟悉classic ASP,我需要用classic ASP编写web应用程序,因为客户希望它使用classic ASP:( 不管怎样!我的问题是: 当我有一个名为person的类的对象时:Session 会话对象中的经典ASP存储对象,session,asp-classic,object-persistence,Session,Asp Classic,Object Persistence,我不熟悉classic ASP,我需要用classic ASP编写web应用程序,因为客户希望它使用classic ASP:( 不管怎样!我的问题是: 当我有一个名为person的类的对象时: Class Person Private m_sFirstName Public Property Get firstName firstName = m_sFirstName End Property Public Property Let firstName(value) m_sF
Class Person
Private m_sFirstName
Public Property Get firstName
firstName = m_sFirstName
End Property
Public Property Let firstName(value)
m_sFirstName = value
End Property
End Class
set aPerson = new Person
Person.firstName = "Danny"
set Session("somePerson") = aPerson
到目前为止还不错 在下一个请求中,我尝试读取会话变量,如下所示:
If IsObject(Session("aPerson")) = true Then
set mySessionPerson = Session("aPerson")
Response.Write(TypeName(myTest)) // will output "Person"
Response.Write(mySessionPerson.firstName) // will output "Object doesn't support this property or method: 'mySessionPerson.firstName'
End If
任何关于未来的想法都会很有帮助。不应该吗
If IsObject(Session("somePerson")) = true Then
set mySessionPerson = Session("somePerson")
我无法解释为什么你的代码不工作,我觉得很好
对象是在脚本上下文中创建的,该脚本上下文在请求完成后被删除。因此,当类型名称可用时,对象的功能被破坏
我可以告诉你,在会话中存储对象不是一个好主意,即使那些不是在脚本中创建的
ASP中使用的大多数对象都存在于单个线程中。一旦创建了对象,只有创建该对象的线程才能访问该对象。为了解决这个问题,在会话中存储了对象后,ASP将会话与创建该对象的特定工作线程关联到该对象
当该会话的后续请求到达时,它现在必须由其特定的工作线程处理。如果该线程恰好忙于处理其他请求,则会话请求将排队,即使存在大量其他可用工作线程
总体效果是破坏应用程序的可伸缩性,工作负载可能会在工作线程之间不均匀地分布。您可以这样做,但必须有点狡猾。我的理解是,当您在会话中存储一个自制对象时,会保留所有数据,但会丢失所有功能。要重新获得该功能您必须“重新水合”会话中的数据 以下是JScript for ASP中的一个示例:
<%@ Language="Javascript" %>
<%
function User( name, age, home_town ) {
// Properties - All of these are saved in the Session
this.name = name || "Unknown";
this.age = age || 0;
this.home_town = home_town || "Huddersfield";
// The session we shall store this object in, setting it here
// means you can only store one User Object per session though
// but it proves the point
this.session_key = "MySessionKey";
// Methods - None of these will be available if you pull it straight out of the session
// Hydrate the data by sucking it back into this instance of the object
this.Load = function() {
var sessionObj = Session( this.session_key );
this.name = sessionObj.name;
this.age = sessionObj.age;
this.home_town = sessionObj.home_town;
}
// Stash the object (well its data) back into session
this.Save = function() {
Session( this.session_key ) = this;
},
this.Render = function() {
%>
<ul>
<li>name: <%= this.name %></li>
<li>age: <%= this.age %></li>
<li>home_town: <%= this.home_town %></li>
</ul>
<%
}
}
var me = new User( "Pete", "32", "Huddersfield" );
me.Save();
me.Render();
// Throw it away, its data now only exists in Session
me = null;
// Prove it, we still have access to the data!
Response.Write( "<h1>" + Session( "MySessionKey" ).name + "</h1>" );
// But not its methods/functions
// Session( "MySessionKey" ).Render(); << Would throw an error!
me = new User();
me.Load(); // Load the last saved state for this user
me.Render();
%>
- 姓名:
- 年龄:
- 家乡:
我会用VB6创建一个COM对象,它看起来像你的Person类。然后存储它。代码非常相似
皮特的方法可能有效。我懒得为你测试它,但是
而不是:
set Session("somePerson") = aPerson
尝试:
如下所示设置会话数据:
set Session.Contents("UserData") = UserData
Session.Contents("UserData.UserIsActive")
然后像这样得到它:
set Session.Contents("UserData") = UserData
Session.Contents("UserData.UserIsActive")
这没关系,因为ASP Classic中的对象无法序列化。很抱歉,我把程序搞砸了example@Jeffery:ASP和sessionobject没有“序列化”的概念。我已经看到了这一点。随着会话和应用程序中对象数量的增加,性能会下降。