JSTL & jsp:useBean life cycle?

P

P.Hill

I guess I don't really understand the life cycle of a session object in a JSP
page. I would have thought that the following would leave the content bean
such that content.init() would only be called once, but all I have to do is
a refresh and I get a new call to init(). Doesn't the useBean
get an old bean if it can find one? Should I be using a <c:set .. > instead?
Please explain.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<HEAD>
<TITLE>Testing</TITLE>
</HEAD>
<BODY>

<jsp:useBean id="content" scope="session" class="Content" />

<c:eek:ut value="${content.test}" />

<c:if test="content.inited" >
<br />It was already initialized.
</c:if >
<c:if test="${!content.inited}" >
<br />It was NOT already initialized.
<% content.init(); %>
<br />But it is now.
</c:if >
</BODY>
</HTML>

Also is there an jstl way to call the init() method, so I don't have
to use a the scriplet:
<% content.init(); %>
?

TIA,
-Paul
 
F

Fotz

I guess I don't really understand the life cycle of a session object in a
JSP
page. I would have thought that the following would leave the content bean
such that content.init() would only be called once, but all I have to do is
a refresh and I get a new call to init(). Doesn't the useBean
get an old bean if it can find one? Should I be using a <c:set .. > instead?
Please explain.

I think useBean creates a new object and reference to object from previous
call is lost and replaced by reference to new object. You can check if such
an object is already created in sessionScope before using useBean.
Also is there an jstl way to call the init() method, so I don't have
to use a the scriplet:
<% content.init(); %>

You can achieve it by something like that:

<c:set target="${content}" property="init" value="some_value">

where ${content} is your reference to Bean created by useBean
And you must have method in your bean called setInit(Some_Type value) so
<c:set> can call it, and you can pass an argument to it.

Greetings
Fotz
 
P

P.Hill

Fotz said:
You can achieve it by something like that:

<c:set target="${content}" property="init" value="some_value">

So there is no way to call a no argument method?
-Paul
 
P

P.Hill

Fotz said:
I think useBean creates a new object and reference to object from previous
call is lost and replaced by reference to new object. You can check if such
an object is already created in sessionScope before using useBean.

That sounds like the behavior I'm seeing, but
I find on page "1-101" (PDF page 137-138) of JSP 2.0 Spec
"Semantics
The actions performed in a jsp:useBean action are:
1. An attempt to locate an object based on the attribute values id and scope.
The inspection is done synchronized per scope namespace to avoid
non-deterministic behavior.
2. A scripting language variable of the specified type (if given) or class (if
type [...] is defined [...]
3. If the object is found, the variable’s value is initialized with a reference
to the located object, cast to the specified type. If the cast fails, a
java.lang.ClassCastException
shall occur. This completes the processing of this jsp:useBean action."

That sounds like it SHOULD find an old object.
Whaz up with that?

Can someone explain?

-Paul
 
C

Chris Riesbeck

P.Hill said:
I guess I don't really understand the life cycle of a session object in a JSP
page. I would have thought that the following would leave the content bean
such that content.init() would only be called once, but all I have to do is
a refresh and I get a new call to init(). Doesn't the useBean
get an old bean if it can find one? ....

<jsp:useBean id="content" scope="session" class="Content" />

<c:eek:ut value="${content.test}" />

<c:if test="content.inited" >
<br />It was already initialized.
</c:if >
<c:if test="${!content.inited}" >
<br />It was NOT already initialized.
<% content.init(); %>
<br />But it is now.
</c:if >

The old-fashioned way to do this was inside the useBean element:

<jsp:useBean ...> <--- NOT />
<%= content.init() %>
</jsp:useBean>

this calls init() only when the bean has to be constructed.

But I don't know why your bean is not being found on refresh.
Something else is going on there. But try the above and see if it
helps or narrows the problem.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top