Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSession with two different copies  RSS feed

 
jason pitt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

GOAL: Is to get the same session attribute value in to two different variables (list1 and list2) and then modify one variable (list1) with the user input through mutiple screens and then if the user wants to cancel his input entry, then use the unmodified variable(list2)

I got a list of DTO which is stored in session.

I am getting that session object in two variables , like

List<DTO> list1 = session.getAttribute("abc");
List<DTO> list2 = session.getAttribute("abc");

If i am updating list1, the values in the list2 is also automatically updated(which I don't want), as I want to keep this copy as the original one. I think, this is because both list1 and list2 points to the same session object. How to I accomplish my goal keeping them separate?

Thanks

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make a copy rather than just an alias.
 
jason pitt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Make a copy rather than just an alias.


Yeah, I tried that too. But did not work

List<DTO> list1 = session.getAttribute("abc");
List<DTO> list2 = list1;

- Above did not work

List<DTO> list1 = session.getAttribute("abc");
List<DTO> list2 = new ArrayList<DTO>();

for (DTO x : list1) {
list2.add(x)
}

- This did not work too

Could you please suggest anything else?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

You're not really thinking this through. If you have an array list of Java objects you have the following:
1. A Java object (the array list)
2. All the Java objects assigned to the array list.

Now, if you just assign the array list to a new variable, you basically create a new reference to the same array list object.

What you did then was to create a new array list object, and create references in it to all the objects currently referenced by the original array list. However, think about what those references are pointing to? Aren't both array lists referencing the same set of Java objects? What you did was create a new array list object, but that's it. They both still point to the same list of objects.

After creating a new array list, you can modify that object (the array list) and this won't affect the original. For example, you can add a new object to the array list, and it won't magically show up in the original array list. But because both array lists still refer to the same set of object, if you modify one of those objects, the change will show in both array lists.

What you're looking for is called deep copy. If you google for it, you should find lots of information.

However, I don't think this is the best approach. Normally, the application would show information that is stored in the database. You can then use the pages in the application to allow the user to modify this information, then when everyone is happy with the modifications, update the database with the changes. If you don't want to keep the changes, just throw them away.

If you need to modify the database during the work flow and still want to revert back to the original, then look into doing the work within a transaction. However, a transaction that spans multiple HTTP requests may not be a good idea.

Good luck.
 
jason pitt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark E Hansen wrote:Jason,

You're not really thinking this through. If you have an array list of Java objects you have the following:
1. A Java object (the array list)
2. All the Java objects assigned to the array list.

Now, if you just assign the array list to a new variable, you basically create a new reference to the same array list object.

What you did then was to create a new array list object, and create references in it to all the objects currently referenced by the original array list. However, think about what those references are pointing to? Aren't both array lists referencing the same set of Java objects? What you did was create a new array list object, but that's it. They both still point to the same list of objects.

After creating a new array list, you can modify that object (the array list) and this won't affect the original. For example, you can add a new object to the array list, and it won't magically show up in the original array list. But because both array lists still refer to the same set of object, if you modify one of those objects, the change will show in both array lists.

What you're looking for is called deep copy. If you google for it, you should find lots of information.

However, I don't think this is the best approach. Normally, the application would show information that is stored in the database. You can then use the pages in the application to allow the user to modify this information, then when everyone is happy with the modifications, update the database with the changes. If you don't want to keep the changes, just throw them away.

If you need to modify the database during the work flow and still want to revert back to the original, then look into doing the work within a transaction. However, a transaction that spans multiple HTTP requests may not be a good idea.

Good luck.


Thanks, Mark. It worked by using deep copy.

FYI - Based on our screen design and the workflow, session tracking is the best way
 
jason pitt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:ItDoesntWorkIsUseless


I am not sure why you got mad ... anyways, thanks for your initial response
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason pitt wrote:
Bear Bibeault wrote:ItDoesntWorkIsUseless


I am not sure why you got mad ... anyways, thanks for your initial response

Who says I got mad?

What I was trying to do was two-fold:

  • Help you get answers faster. "It doesn't work" isn't a good way to describe the problem. Read the link. It will help you get better responses.


  • I was hoping that in the process of you trying to describe the problem, that you'd slap yourself on the forehead and realize "oh wait! I'm just making a list that points to the same objects!"

  •  
    jason pitt
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:
    jason pitt wrote:
    Bear Bibeault wrote:ItDoesntWorkIsUseless


    I am not sure why you got mad ... anyways, thanks for your initial response

    Who says I got mad?

    What I was trying to do was two-fold:

  • Help you get answers faster. "It doesn't work" isn't a good way to describe the problem. Read the link. It will help you get better responses.


  • I was hoping that in the process of you trying to describe the problem, that you'd slap yourself on the forehead and realize "oh wait! I'm just making a list that points to the same objects!"



  • Thanks for the clarification
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!