Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Session variables weird behavior

Thread view: 
Nino - 17 May 2007 23:58 GMT
Hello,

I have a double array that I store in the session variable: finalList[]
[]

String[][] finalList = new String[8][25];
... do some work to pull information into double array ...
session.setAttribute("array.fl", finalList);

All good so far. I can call the array from anywhere in my program
without having the pull the information and sort it again. Now, I am
building a piece that allows a user to edit the information, but if
they don't like the changes they can go back to how it was. Thus I
decided to build a new array with a similar structure to hold the
information temporarily. Once the final "Save" button has been
clicked, it will store the temporary array information into the
finalList... like this:

String[][] editFinalList = new String[8][25];
if (session.getAttribute("array.efl") == null) {
    editFinalList = (String[][]) session.getAttribute("array.fl");
    session.setAttribute("array.efl", editFinalList);
} else {
    editFinalList = (String[][]) session.getAttribute("array.efl");
}

Here's the problem... Any changes I make to editFinalList are
automatically stored in the session variable for finalList!! I wrote
the following code (on a completely different Servlet) to see what was
in all my session variables:

String finalList[][] = (String[][]) session.getAttribute("array.fl");
for (int x=0; (x < finalList[0].length) && (finalList[0][x] != null); x
++) {
    out.println(""+finalList[0][x]+" ... etc ... ");
}

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
run the above code, it shows that changes were made to the finalList
array. How is this possible? Is there something I'm missing or don't
understand completely? Should I try another approach to editing data
and storing it temporarily?

I'm pulling my hair out here and appreciate any help. Thanks,
Nino Skilj
Tom Hawtin - 18 May 2007 00:21 GMT
> 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
> run the above code, it shows that changes were made to the finalList
> array. How is this possible? Is there something I'm missing or don't
> understand completely? Should I try another approach to editing data
> and storing it temporarily?

You're not making a copy of the object, so you just end up with two
references to the same instance.

Consider:

char[] a = 'x';
char[] b = a;
b[0] = 'y';
System.out.println(a[0]);

What would you expect? You only have one char array, so /a/ is mutated.

The thing to do here is to change the second line to:

char[] b = a.clone();

In your example, you would need to copy all the element arrays as well
as the main array.

Tom Hawtin
Nino - 18 May 2007 01:05 GMT
> > 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.