Classic ASP Store objects in the session object
Asked Answered
L

6

5

I am new to classic ASP and I need to code a web application in classic asp because the customer wants it to be in classic asp. :(

Anyways! here is my question:

When I have a object of a class 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...

On the next request , I try to read the session var like :

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 about what is going would be of great help.

Liveryman answered 28/7, 2009 at 20:26 Comment(0)
S
5

I can't explain why your code doesn't work looks fine to me.

The object is created in a script context which is subsequently torn down after the request is complete. Hence whilst the type name is available the object's function is broken.

I can tell you its not a good idea to store objects in the Session even those not created in script.

Most objects used in ASP exist in a single thread. Once created only the thread that created the object may access the object. To cope with this once you have stored an object in the session ASP affiliates the session with the specific worker thread that created it the object.

When a subsequent request for that session arrives it must now be handled by its specific worker thread. If that thread happens to be busy working for some other request the sessions request is queued, even if there a plenty of other available worker threads.

The overall effect is to damaging the applications scalability where the work load can become unevenly distributed across the worker threads.

Swagger answered 29/7, 2009 at 16:9 Comment(1)
I've seen this. Performance is degraded with an increasing number of objects in Session and Application.Immobilize
A
2

You can do this but you have to be a bit sneaky. My understanding of it is when you store a home baked object in Session is keeps all the data but loses all the functionality. To regain the functionality you have to "re-hydrate" the data from the session.

Heres an example in 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();

%>

Its quite a powerful method of managing saving state into the Session and can easily be swopped out for DB calls/XML etc. if needed.

Interesting what Anthony raises about threads, knowing his depth of knowledge I'm sure its correct and something to think about but if its a small site you will be able to get away with it, we've used this on a medium sized site (10K visitors a day) for years with no real problems.

Abeyant answered 30/7, 2009 at 13:33 Comment(0)
G
1

Shouldn't it be

If IsObject(Session("somePerson")) = true Then
    set mySessionPerson = Session("somePerson")
Gunwale answered 28/7, 2009 at 20:28 Comment(3)
It doesn't matter since objects in ASP Classic can't be serialized.Giulietta
Sorry i messed up in the exampleLiveryman
@Jeffery: ASP and the sessionobject carries no concept of "serialisation".Swagger
I
0

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

Pete's method probably works.

Immobilize answered 30/7, 2009 at 13:52 Comment(0)
T
0

Set session data like this:

set Session.Contents("UserData") = UserData

and then get it like this:

Session.Contents("UserData.UserIsActive")
Transistorize answered 30/12, 2012 at 23:12 Comment(0)
C
-1

I am to lazy to test it for you, but

Instead of:

set Session("somePerson") = aPerson

Try:

Set Session("somePerson") = Server.CreateObject(aPerson)
Corum answered 3/12, 2010 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.