Classic ASP Store objects in a session object

I am new to classic ASP, and I need to encode the web application in classic asp because the client wants it to be in classic asp. :(

Anyway! here is my question:

When I have a class object called person:

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

So far so good ...

In the following query, I try to read the var session as follows:

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

Any ideas on what is going on will be very helpful.

+5
source share
6 answers

I can’t explain why your code is not working, it looks great.

The object is created in the context of the script, which subsequently breaks down after the request is completed. Therefore, while the type name is available, the function of the object is violated.

, , script.

, ASP, . , , . , , ASP , .

, . , - , , .

, .

+3

, . - , , . , " " .

JScript 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();

%>

DB/XML .., .

, , , , - , , (10 . ) .

+2

If IsObject(Session("somePerson")) = true Then
    set mySessionPerson = Session("somePerson")
+1
source

I would create a COM object that looks like your Person class with VB6. Then save it. The code is very similar.

Perhaps the Pete method works.

0
source

Set the session data as follows:

set Session.Contents("UserData") = UserData

and then get the following:

Session.Contents("UserData.UserIsActive")
0
source

I have to be lazy to check it out for you, but

Instead:

set Session("somePerson") = aPerson

Try:

Set Session("somePerson") = Server.CreateObject(aPerson)
-1
source

All Articles