Clearing properties of Object in session scope.

R

Rico

I'm kind of puzzled here.

For
<jsp:useBean id="obj" scope="session" class="obj.XX" />
<jsp:setProperty name="obj" property="*" />

After some requests, obj.total = 7000

I have a 'New obj' button that I am counting on to reset certain values
and reload the current page in which there's the following code:

System.out.println("1 - Value of obj.total=" + obj.total);
obj = new XX();
System.out.println("2 - Value of obj.total=" + obj.total);

As expected, output is:
1 - Value of obj.total=7000
2 - Value of obj.total=0 // reset in constructor

But if I click 'New obj' button right away again, I don't get
1 - Value of obj.total=0
2 - Value of obj.total=0

but
1 - Value of obj.total=7000
2 - Value of obj.total=0

How can this be? Has the constructor call not been committed to the
session scope when I did "obj = new XX();" ?
Do I have to clear the properties in a dedicated method then?

Rico.
 
R

Ryan Stewart

Rico said:
I'm kind of puzzled here.

For
<jsp:useBean id="obj" scope="session" class="obj.XX" />
<jsp:setProperty name="obj" property="*" />

After some requests, obj.total = 7000

I have a 'New obj' button that I am counting on to reset certain values
and reload the current page in which there's the following code:

System.out.println("1 - Value of obj.total=" + obj.total);
obj = new XX();
System.out.println("2 - Value of obj.total=" + obj.total);

As expected, output is:
1 - Value of obj.total=7000
2 - Value of obj.total=0 // reset in constructor

But if I click 'New obj' button right away again, I don't get
1 - Value of obj.total=0
2 - Value of obj.total=0

but
1 - Value of obj.total=7000
2 - Value of obj.total=0

How can this be? Has the constructor call not been committed to the
session scope when I did "obj = new XX();" ?
Do I have to clear the properties in a dedicated method then?
I don't see anywhere in there that you store a reference to the new object in
the session.
 
R

Rico

I don't see anywhere in there that you store a reference to the new
object in the session.

care to provide a hint how to do that?
google for 'store reference object session jsp' led me to try

session.setAttribute("obj", new XX());

after removing the line ' obj = new XX(); '

but it only seems to make things worse. Thanks.

Rico.
 
J

John C. Bollinger

Rico said:
I'm kind of puzzled here.

For
<jsp:useBean id="obj" scope="session" class="obj.XX" />

This declares that your page is going to use a bean of class obj.XX,
bound to session scope, and referred to by a scripting variable named
"obj". If such a bean already exists then it will be used, otherwise a
new one will be created and bound to the session.
<jsp:setProperty name="obj" property="*" />

This sets the properties of the bean referred to by scripting variable
"obj" based on the parameters of the current request, by matching up
parameter names with property names.
After some requests, obj.total = 7000

I have a 'New obj' button that I am counting on to reset certain values
and reload the current page in which there's the following code:

System.out.println("1 - Value of obj.total=" + obj.total);
OK.

obj = new XX();

This is your problem. It assigns a new bean instance to the scripting
variable set up for your session-scope bean. That just causes the
scripting variable to refer to the new instance from that point forward
within its scope. That is DIFFERENT from associating the new bean with
the chosen name in the larger scope provided by the session. To do so,
you would do something like

session.setAttribute("obj", obj);

On the other hand, it might make more sense to give the bean class a
reset() method that clears instance state.
 
R

Ryan Stewart

Rico said:
care to provide a hint how to do that?
google for 'store reference object session jsp' led me to try

session.setAttribute("obj", new XX());

after removing the line ' obj = new XX(); '

but it only seems to make things worse. Thanks.
As John said, that's what you want to do, though it would probably be better to
have a reset method to reset the total. Why do you say it makes things worse?
 
R

Rico

As John said, that's what you want to do, though it would probably be
better to have a reset method to reset the total. Why do you say it
makes things worse?

It was a matter of seeing it as there being
[1] a 'label' that remains in the session and
[2] a 'pointer/reference/variable' in the current scope (page, I think)
They are two different ways of accessing/modifying what is supposed to be
the same memory space.

obj = new XX();
didn't change the session scope value/memory space that 'obj' key points
to.

session.setAttribute("obj", new XX());
didn't point page(?) scope obj to the new area of memory

obj = new XX();
session.setAttribute("obj, obj);
works fine.

Rico.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top