Discussions

Web tier: servlets, JSP, Web frameworks: Session variables

  1. Session variables (3 messages)

    Hi there,

    i'm quite new to java and jsp but i want to do the following:

    I have the following class:

    class DataSet
    {
        private vector Records;
        private int RecordCount;
        ...
    }

    class Record
    {
        private vector ColValues;
    }

    When saving the dataset to session using putValue("ds",aDataSet);

    will the putValue also save all the records in the vector to the session????

    Any help is appreciated
    Hope you can help me oput Thanx Rim

    Threaded Messages (3)

  2. Session variables[ Go to top ]

    Yes, putValue should save each record in the Vector as well. I would suggest that you have both of your classes also implement Serializable. Also, be sure that any object you put in the Vector also implements Serializable.

    One other suggestion. If you're using a servlet container that supports Servlet 2.2, then use setAttribute() instead of putValue(), as putValue() has been deprecated.

    Good luck.

    Andy
  3. Session variables[ Go to top ]

    Hi,

    thanx for the reply, but what does implements serializable do...

    I used HttpSession mysession = request.getSesion(true); to get my session. But what's the difference except that HttpSession has no function for set and getAttribute

    Thanx Rim
  4. Session variables[ Go to top ]

    Implementing Serializable just marks your class as being serializable by the VM. You have to ensure that all instance variables of your class are also serializable. All primitive types in Java are serializable as is String. Any objects that are a part of your class should also implement serializable, or otherwise marked as transient.

    getAttribute() and setAttribute() were introduced with the Servlet 2.2 spec, so you if you're using a container that isn't 2.2 compliant, then you should use putValue() and removeValue(). The only container I know of that isn't 2.2 compliant is WebSphere.

    Andy