Attribute being set unknowingly

Hi i am working on a site that generates reports. Some users [Admins] have capability of 'modifying' the report request submitted by other users. When the Admin clicks the 'modify' button i am setting some attributes.

these two are of importance in regards to my Q:

userSession.setAttribute("selectedPros", selectedPros);//where selectedPros is a list

userSession.setAttribute("initialRequestedPros", selectedPros);

all i am doing is trying to save the Pros requested by a NON admin user into initialRequestedPros. I am doing this so if the Admin tries to modfy the initial # of selectedPros he gets like a warning message.

So the problem i am having is that when some changes are being made to selectedPros list. This changes are affecting the initialRequestedPros attribute as well. so when finally the Admin clicks SUBMIT after modifying. I have no way of telling whether he made changes to the initial request submitted by the user. I cant understand as to WHY the same changes are reflecting on initialRequestedPros. Attribute :(

Thanks

Message was edited by:

bhaarat_java

This will clear some confusions to you guys:

So the code is this:

List selectedPros = (List) userSession.getAttribute("selectedPros");

System.out.println("This is initialRequestedPros in buildSelected 0" + userSession.getAttribute("initialRequestedPros") );

selectedPros.clear();

System.out.println("This is initialRequestedPros in buildSelected 1" + userSession.getAttribute("initialRequestedPros ") );

/*This is the output i got*/

[11/26/06 12:42:13:415 EST] 1817cf0a SystemOutO This is initialRequestedPros in buildSelected 0[100001 Bhaarat Sharma]

[11/26/06 12:42:13:415 EST] 1817cf0a SystemOutO This is initialReqProvider in buildSelected 1[]

[11/26/06 12:42:13:415 EST] 1817cf0a SystemOutO This is initialReqProvider in buildSelected 2[052803 Some other Name]

This is really really weird to me

[2232 byte] By [bhaarat_javaa] at [2007-10-3 10:50:32]
# 1

You are still using the same object to hold selectedPros and initialRequestedPros. The two different names are just references to the same object.

If you want to make one independent of the other, then you should do a Copy, for which ArrayList has a copy constructor:

userSession.setAttribute("selectedPros", new ArrayList(selectedPros));

userSession.setAttribute("initialRequestedPros", selectedPros);

In this case, initialRequestedPros would be the original order List object. The selectedPros would be a copy of the List, a new Object that if you modify then you would not affect the initialRequestedPros object.

stevejlukea at 2007-7-15 6:15:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...