質問編集履歴
1
試した結果の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
もし、後者であればHttpSession#getAttribute()で取得した値を変更したとき、HttpSession#setAttribute()は不要という認識で良いのでしょうか?
|
7
7
|
|
8
|
+
**下のほうに追記#1を追加しました**
|
9
|
+
|
8
10
|
```java
|
9
11
|
FooBean foo = new FooBean();
|
10
12
|
foo.hoge = "foo";
|
@@ -18,4 +20,27 @@
|
|
18
20
|
// *このコードは不要?
|
19
21
|
session.setAttribute("Foo", tmp);
|
20
22
|
|
23
|
+
```
|
24
|
+
|
25
|
+
**追記#1**
|
26
|
+
実際に試してみました。
|
27
|
+
その結果、tmp1を書き換えた後にsetAttribute()を実行していないのにも関わらずtmp2の値に反映されていることが分かりました。
|
28
|
+
|
29
|
+
ということは、getAttribute()で取得した値は同じインスタンスを指すのでsetAttribute()は不要なのかなと思いました。
|
30
|
+
ただ、回答の中でSerializableに関して書かれており、私はそのあたりが詳しくないので、もしかしたら別のインスタンスを指すこともあるのでしょうか。
|
31
|
+
|
32
|
+
```
|
33
|
+
FooBean foo = new FooBean();
|
34
|
+
foo.hoge = "foo";
|
35
|
+
|
36
|
+
HttpSession session = request.getSession();
|
37
|
+
session.setAttribute("Foo", foo);
|
38
|
+
|
39
|
+
FooBean tmp1 = (FooBean)session.getAttribute("Foo");
|
40
|
+
System.out.println(tmp1.hoge); // 出力結果:"foo"
|
41
|
+
tmp1.hoge = "hoge";
|
42
|
+
|
43
|
+
FooBean tmp2 = (FooBean)session.getAttribute("Foo");
|
44
|
+
System.out.println(tmp2.hoge); // 出力結果:"hoge"
|
45
|
+
System.out.println(foo.hoge); // 出力結果:"hoge"
|
21
46
|
```
|