重新分配Java会话对象 [英] Java session object is reassigned

查看:105
本文介绍了重新分配Java会话对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在会话中设置项目列表来向JSP发送。我在这里使用Spring控制器。

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

List<Item> l = new ArrayList<Item>();
l.add("item1");
l.add("item2");
l.add("item3");
session.setAttribute("fisrt_item_list", l); // So the fisrt_item_list 

会话对象包含所有三个项目。

The session object contains all three items.

...
...

// Somewhere down the code, l.remove(2); //removing item3.

return "some_page";

问题:

预期: fisrt_item_list 会话对象应包含3个项目。

Expected: fisrt_item_list session object should contain 3 items.

实际:会话中只有2项可用,因为删除第三项更新了会话对象。但我从未将更新的l对象添加到会话中。为什么java在返回页面之前重新分配会话对象?那么,我是否需要将l复制到另一个列表并使用它?

Actual : Only 2 items available in session as the removal of third item updated the session object. But I have never added the updated l object into session. Why does java reassign the session object before returning a page? So, do I need to copy over the l to another list and use that?

推荐答案

您必须先了解值类型和引用类型,请检查以下链接 StackOverflow

You must first understand the difference between Value Type and Reference Type in Java , check the following link StackOverflow


让我举一个值类型的简单示例

let me give you a simple example of Value Type

MAIN.java:

class Main{
     public static void main(String [] args){
       C1 obj1 = new C1();
       obj1.print();
       obj1.increment();
       obj1.print();
     }
}

C1.java:

class C1{
  int a = 10;
  int b = 20;

  public C1(){
  }
  void increment(){
      System.out.println("incrementing a and b");
      new C2().inc(a,b);
  }
  void print(){
    System.out.println("Value of a "+ a);
    System.out.println("Value of b "+ b);
  }
}

C2.java

class C2{
   void inc(int a, int b){
      a++;
      b++;
   }
}

输出

Value of a 10
Value of a 20
incrementing a and b
Value of a 10
Value of a 20

结论

int a int b 的值C1 class 没有增加,因为在java中是一个原始类型(恰好是一个值类型)

A真实世界的示例到值类型可以如下:

如果我给你我的标记表的照片副本,如果你涂鸦,原始标记表不会发生任何事情,因为它是我的标记表的副本类似地,当你传递变量int a和int b时,这些变量的副本被发送到inc函数,因此它不会增加 int a 和<$ c $中的原始值。 c> int b 类C1

The value of int a and int b in C1 class was not incremented because in is a primitive type in java(which happens to be a value type)
A real world example to value type can be as follows :
If I give you photo copy of my mark sheet and if you scribble on it nothing will happen to original mark sheet because it is a copy of my mark sheet similarly when you pass the variable int a and int b , copy of those variables is sent to the inc function hence it doesnot increment the original value in int a and int b of class C1


让我们看一个参考类型的示例

let us see an example for reference type

MAIN.java:

class Main{
     public static void main(String [] args){
       C1 obj1 = new C1();
       obj1.print();
       C2.inc(obj1);
       obj1.print();
     }
}

C1.java:

class C1{
  int a = 10;
  int b = 20;

  public C1(){
  }

  void print(){
    System.out.println("Value of a "+ a);
    System.out.println("Value of b "+ b);
  }
}

C2.java

class C2{
   public static void inc(C1 obj1){
      obj1.a++;
      obj1.b++;
   }
}

输出

Value of a 10
Value of a 20
incrementing a and b
Value of a 11
Value of a 21

结论

这里int a和int b的原始值递增,因为对该对象的引用被传递

当我们说 C1 obj1 = new时C1(); 关键字new在堆内存中生成类C1 的对象,并且对该对象的引用存储在<$ c中$ c>变量obj1 类型C1 obj1 不是它的对象处理堆中创建的对象C1 ,所以当我们将 obj1 传递给 inc时( C1 obj1)类C2 我们传递对象的句柄而不是对象,对象在堆中

所以这不是对象的副本,但原始对象是因为句柄obj1而传递的(使用ref现实世界,例如前面给出的)

here the original value of int a and int b is incremented , because the reference to that object is passed
When we say C1 obj1 = new C1(); the key word new makes an object of class C1 on the heap memory and the reference to that object is stored in variable obj1 of type C1 , obj1 is not the object it is a handle to the Object of C1 created in heap, so when we pass obj1 to inc(C1 obj1) in class C2 we pass the handle of the object not the object , the object is in the heap
So this is not the copy of the object but the original object is passed because of the handle obj1(with ref to real world eg given earlier)


回答主要问题

Answer to the main question



List<Item> l = new ArrayList<Item>();
l.add("item1");
l.add("item2");
l.add("item3");
session.setAttribute("fisrt_item_list", l);

在这里设置 ArrayList对象 会话对象,但是对象不是字面上存储在会话对象中,Arraylist对象的句柄存储在会话对象中,所以当从Arraylist中删除一个值时对象它会反映在会话对象中,因为您的会话对象包含Arraylist对象的句柄,该对象仍然指向从中删除该值的堆上的同一对象

here you set the ArrayList object in the session object ,but object is not literally stored in the session object , the handle to the object of Arraylist is stored in the session object , so when a value is deleted from the Arraylist object it is reflected in the session object because your session object holds the handle to the Arraylist object which is still pointing to the same object on the heap from which the value is deleted


注意:一个对象持有对另一个对象的引用称为对象组合,请检查此链接 StackOverflow

这篇关于重新分配Java会话对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆