質問編集履歴

1

試した結果の追加

2018/09/13 03:25

投稿

sakata_inu
sakata_inu

スコア25

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,10 @@
9
9
 
10
10
 
11
11
  もし、後者であればHttpSession#getAttribute()で取得した値を変更したとき、HttpSession#setAttribute()は不要という認識で良いのでしょうか?
12
+
13
+
14
+
15
+ **下のほうに追記#1を追加しました**
12
16
 
13
17
 
14
18
 
@@ -39,3 +43,49 @@
39
43
 
40
44
 
41
45
  ```
46
+
47
+
48
+
49
+ **追記#1**
50
+
51
+ 実際に試してみました。
52
+
53
+ その結果、tmp1を書き換えた後にsetAttribute()を実行していないのにも関わらずtmp2の値に反映されていることが分かりました。
54
+
55
+
56
+
57
+ ということは、getAttribute()で取得した値は同じインスタンスを指すのでsetAttribute()は不要なのかなと思いました。
58
+
59
+ ただ、回答の中でSerializableに関して書かれており、私はそのあたりが詳しくないので、もしかしたら別のインスタンスを指すこともあるのでしょうか。
60
+
61
+
62
+
63
+ ```
64
+
65
+ FooBean foo = new FooBean();
66
+
67
+ foo.hoge = "foo";
68
+
69
+
70
+
71
+ HttpSession session = request.getSession();
72
+
73
+ session.setAttribute("Foo", foo);
74
+
75
+
76
+
77
+ FooBean tmp1 = (FooBean)session.getAttribute("Foo");
78
+
79
+ System.out.println(tmp1.hoge); // 出力結果:"foo"
80
+
81
+ tmp1.hoge = "hoge";
82
+
83
+
84
+
85
+ FooBean tmp2 = (FooBean)session.getAttribute("Foo");
86
+
87
+ System.out.println(tmp2.hoge); // 出力結果:"hoge"
88
+
89
+ System.out.println(foo.hoge); // 出力結果:"hoge"
90
+
91
+ ```