Java session object is reassigned

后端 未结 2 784
栀梦
栀梦 2021-01-16 13:13

I am trying to send List of Items to JSP by setting them in a session. I am using Spring controller here.

List l = new ArrayList();
         


        
2条回答
  •  既然无缘
    2021-01-16 13:43

    Java uses reference assignment. In your case when you assigns a list object to session attribute, session does not store a copy of that list object, instead it just stores the reference of the list object.

    So any modification in that list object will reflect to session as well. If you don't want to update session, create a new list out of the existing list like below:

    List list=new ArrayList(l);

提交回复
热议问题