need to do session.setAttribute() after updating the session?

J

jrefactors

There are 2 jsp pages. In page1.jsp, it will keep the session sa, and
it can go to page2.jsp
If page2.jsp will modify the session, do we need to set the session
again, to make the session
updated? i.e. do we need this: session.setAttribute("sa", sa);

page1.jsp
==========

session.setAttribute("sa", sa);


page2.jsp
=========
sa = (ServiceAddressModel)session.getAttribute("sa");
sa.setName("joe");
session.setAttribute("sa", sa); //do we need this??
please advise. thanks!!
 
R

Ryan Stewart

There are 2 jsp pages. In page1.jsp, it will keep the session sa, and
it can go to page2.jsp
If page2.jsp will modify the session, do we need to set the session
again, to make the session
updated? i.e. do we need this: session.setAttribute("sa", sa);

page1.jsp
==========

session.setAttribute("sa", sa);


page2.jsp
=========
sa = (ServiceAddressModel)session.getAttribute("sa");
sa.setName("joe");
session.setAttribute("sa", sa); //do we need this??
please advise. thanks!!
No you do not. Do you understand the concept of object references or
pointers? This line:
session.setAttribute("sa", sa);
stores a reference to an object in the session.

This line:
sa = (ServiceAddressModel)session.getAttribute("sa");
gets that same reference out of the session.

However all three references (sa in page1, the one in the session, and sa in
page2) point to the same object. For example:
ArrayList a = new ArrayList();
ArrayList b = a;
ArrayList c = b;
a.add("foo");

How many elements does a have in it? What about b? And c? The correct answer
to all three is "one element". This is because a, b, and c are three
different reference variables that refer to the same ArrayList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top