表題の通りです。
HttpSession#getAttribute()で取得した値は、セッションオブジェクトとは別のインスタンスを指すものですか?
それともセッションオブジェクトを指すものですか?
もし、後者であればHttpSession#getAttribute()で取得した値を変更したとき、HttpSession#setAttribute()は不要という認識で良いのでしょうか?
下のほうに追記#1を追加しました
java
1FooBean foo = new FooBean(); 2foo.hoge = "foo"; 3 4HttpSession session = request.getSession(true); 5session.setAttribute("Foo", foo); 6 7FooBean tmp = (FooBean)session.getAttribute("Foo"); 8tmp.hoge = "hoge"; 9 10// *このコードは不要? 11session.setAttribute("Foo", tmp); 12
追記#1
実際に試してみました。
その結果、tmp1を書き換えた後にsetAttribute()を実行していないのにも関わらずtmp2の値に反映されていることが分かりました。
ということは、getAttribute()で取得した値は同じインスタンスを指すのでsetAttribute()は不要なのかなと思いました。
ただ、回答の中でSerializableに関して書かれており、私はそのあたりが詳しくないので、もしかしたら別のインスタンスを指すこともあるのでしょうか。
FooBean foo = new FooBean(); foo.hoge = "foo"; HttpSession session = request.getSession(); session.setAttribute("Foo", foo); FooBean tmp1 = (FooBean)session.getAttribute("Foo"); System.out.println(tmp1.hoge); // 出力結果:"foo" tmp1.hoge = "hoge"; FooBean tmp2 = (FooBean)session.getAttribute("Foo"); System.out.println(tmp2.hoge); // 出力結果:"hoge" System.out.println(foo.hoge); // 出力結果:"hoge"

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/13 03:32