Milhous
Milhous

Reputation: 14653

Using a jsp bean in a session

I am using a JSP bean and when I do an assignment to a new object, it gets over-written on a submit to the previous object.

<jsp:useBean id="base" class="com.example.StandardBase" scope="session" />
...
//base object id  = 396
base = new Base()
//base object id = 1000

and on a resubmit of the page I get

<jsp:useBean id="base" class="com.example.StandardBase" scope="session" />
//base object id = 396

Is there a way to tell JSP to do a new assignment?

Upvotes: 1

Views: 3258

Answers (3)

sblundy
sblundy

Reputation: 61434

I'm not completely sure, but I think base = new Base() does not update the reference stored in the session scope. Therefore, the bean you created with the initial <jsp:useBean/> is still around while the one you create manually, and then updated, isn't.

Get rid of base = new Base() and you should be fine.

If you insist upon updating it, you use HttpSession.setAttribute(). Like so:

session.setAttribute("bean", bean);

I believe the variable session is automatically created and initialized for you by the JSP engine.

Upvotes: 3

krosenvold
krosenvold

Reputation: 77231

You're not supposed to new the bean yourself. Let JSP do that for you

Upvotes: 1

Tim Howland
Tim Howland

Reputation: 7970

would changing scope from session to request fix this for you?

Upvotes: 0

Related Questions