> > Once I add the information to the editFinalList array, nowhere do I
> > make a reference to finalList. However, it still updates it and when I
[quoted text clipped - 23 lines]
>
> Tom Hawtin
In the process, I copied a session evaluation servlet someone had
written and noticed that the session variable to the array was just a
pointer to it. Much like you helped me realize (Tom... thank you!),
all I was doing was making a call to the same reference... I simply
copied all the data into a completely new array and stored that as the
new editFinalList variable.
One other question arises from all this... If I were to store a
session variable as a string like this:
String name = (String) session.getAttribute("name");
I could manipulate and use that string as much as I want, but the
session variable won't change. Why is it then, that when I make a
similar call to a string array like this:
String finalList[][] = (String[][]) session.getAttribute("array.fl");
... it actually changes the data stored in the session? Once I make a
change to finalList in the code, it automatically changes the stored
session variable without me having to do a setAttribute call. However,
that doesn't happen with the "name" string. If I make changes to name,
I have to do a setAttribute again to "save" the information.
I think it's because the session variable just stores a reference to
the actual array, but where is that array stored? Isn't this
inconsistent, or is that how it's supposed to work?
Thanks again for your help Tom!
Nino
Tom Hawtin - 18 May 2007 04:11 GMT
> I could manipulate and use that string as much as I want, but the
> session variable won't change. Why is it then, that when I make a
[quoted text clipped - 6 lines]
> that doesn't happen with the "name" string. If I make changes to name,
> I have to do a setAttribute again to "save" the information.
You can't actually change the String. Once you create a String you are
stuck with its value. So you cannot be doing the same thing to a String
that you are doing to an array.
If you mutate an object saved in the session, you should do a
setAttribute anyway (I think - I haven't done much Servlet stuff over
the last few years). Servlet containers vary implementation, so what
appears to work for you might not work on a different version. It might
sometimes work on a particular container, and sometimes not. The problem
is, the container might copy the data using serialisation, to persist a
session, reduce memory or transfer within a cluster. If you just use
getAttibute, it might not consider the value changed, and so not update.
Tom Hawtin