Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp session

Status
Not open for further replies.

TomArrow

Programmer
Aug 2, 2001
5
US
i'm trying to build an undo method for an html form submission. the form is submitted and prior to the new data being added to an object, i copy that object to the session. then, i make the changes. when i call the undo method and retrieve that unchanged object from the session, it comes out looking like the changed object...the changes magically copied themselves to the session.

the update and undo functions are both in a servlet...session is passed in via pageContext.

i've written a test.jsp where i have a string x="4", i copy that to the session, set the string x="3" and then grab the string from the session and display it...and get 4. so making a change to the object after copying it to the session shouldn't change the object in the session.

what gives!

i'm at wit's end...
 
okay...think i understand why that does work, but here's what i've done to "fix" it...which ofcourse still doesn't work

the object i want to copy to the session is a vector. i tried using the vector.clone() method, but without any luck...never really used it, so must be doin it wrong. so i made my own cloneVector method...looks like this
Code:
public static Vector cloneVector(Vector original)
{  Vector returnVector = new Vector();
   for (int i = 0; i < original.size(); i++)
     returnVector.addElement(original.elementAt(i));
   return returnVector;
}
so the aim of this was to create a new vector with no reference to the original one. it's this new vector that i want to now copy to the session so that it will be preserved from all changes i make to the original.

BUT IT DOESN'T WORK

5 hours...
 
It is much easier than that. Try:
Code:
Vector original = new Vector();
/* Add a bunch of stuff */

Vector vectorClone= original.clone();

Your String example worked because String are immutable objects. Meaning that once a String is created in memory it cannot be changed...EVER! This is also a reason why many people prefer StringBuffer. In your example:
Code:
i've written a test.jsp where i have a string x=&quot;4&quot;, i copy that to the session, set the string x=&quot;3&quot; and then grab the string from the session and display it...and get 4.  so making a change to the object after copying it to the session shouldn't change the object in the session.
- x was pointing to the immutable String &quot;4&quot;
- the session attribute was set to also point at &quot;4&quot;
- x was set to &quot;3&quot; but since Strings are immutable it was really just changed to point to a new String &quot;3&quot;
- the session attribute however was still pointing to the original String &quot;4&quot;

I hope this helps clear things up for you.
Wushutwist
 
Sorry I forgot to cast the clone to a Vector. It should read:
Code:
Vector original = new Vector();
/* Add a bunch of stuff */

Vector vectorClone= (Vector)original.clone();
Wushutwist
 
thank you for you help, but this problem still isn't solved...let me show you a bit of test code that demonstrates the problem
Code:
Vector a = new Vector();
Vector b = new Vector();
Vector c = new Vector();

//grab my object from session and isolate personGroup
DBObject dbo = (DBObject)session.getAttribute(&quot;dbo&quot;);
TransferRecord tr = dbo.getTransferRecord();
a = (Vector)tr.dSOTDataTransfer().personGroup();

//clone a into b so that b is seperate from any changes made
//to a
b = (Vector)a.clone();

//display a data
Person person = (Person)a.elementAt(0);
MailingAddressData mad = person.mailingAddressData();
System.out.println(&quot;\nbefore: &quot;+mad.MA_LN1_TX());

//change a data and display change
mad.setMA_LN1_TX(&quot;9&quot;);
System.out.println(&quot;\nafter: &quot;+mad.MA_LN1_TX());

//display b data
Person p = (Person)b.elementAt(0);
MailingAddressData m = p.mailingAddressData();
System.out.println(&quot;\nb: &quot;+m.MA_LN1_TX());
so assuming that the a data held '0', i would expect to see
Code:
before: 0

after: 9

b: 0
but instead, it's
Code:
before: 0

after: 9

b: 9

grrrrrrrr
 
Here is what the Jave API says for the clone() method in Vector:
Code:
public Object clone()

Returns a clone of this vector. The copy will contain a reference to a clone of the internal data array, not a reference to the original internal data array of this Vector object.

Overrides:
clone in class Object

Returns:
a clone of this vector.

Do your Data Objects implement Cloneable and override the clone() method in your Data objects? If not then maybe that is what is causing you problems. Vector.clone() should implicitly call the clone() of each of its objects. The problem is that if your Data Objects don't support this then they probably don't REALLY get cloned.

Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top