JSP object questions

D

Dave

I am still having problems with objects surviving beyond the execution of
the current page (i.e. they are showing up on subsequent requests for the
same page). I have some questions relating to this:

1) When an object is declared within <%! %>, what scope does that object
have? e.g.

<%!
String string1 = "This is a String";
Vector vec1 = new Vector();
%>

How are objects instantiated using the above method treated differently from
<jsp:useBean ...> objects?

2) When an object is declared using <jsp:useBean... > and it's assigned
"page" scope, when is that object cleaned up? Does it have to be explicitly
removed (by using obj=null) or is it garbage collected when the page is
finished being processed?


3) Is there any way to ensure that when an object is instantiated within a
JSP page, it will no longer exist once that page has finished being
processed by the container (i.e. Tomcat in my case)?



TIA.
 
L

Lew

Dave said:
I am still having problems with objects surviving beyond the execution of
the current page (i.e. they are showing up on subsequent requests for the
same page). I have some questions relating to this:

1) When an object is declared within <%! %>, what scope does that object
have? e.g.

<%!
String string1 = "This is a String";
Vector vec1 = new Vector();
%>

It is an instance member of the JSP's generated servlet class. I do not know
how that affects scope, since the container is able to re-use servlet objects
to handle separate requests. I do not know if the container is supposed to
reset such values on re-use.
How are objects instantiated using the above method treated differently from
<jsp:useBean ...> objects?

The "useBean" variables are method-level variables within the servlet's
service() method. The objects to which they point live in various data
structures depending on scope.
2) When an object is declared using <jsp:useBean... > and it's assigned
"page" scope, when is that object cleaned up? Does it have to be explicitly
removed (by using obj=null) or is it garbage collected when the page is
finished being processed?

The variable goes out of scope at the conclusion of the service() method.
When the object is cleaned up depends on whether other references to it exist,
when the GC feels like checking, and whether the GC feels like reclaiming an
eligible object.
3) Is there any way to ensure that when an object is instantiated within a
JSP page, it will no longer exist once that page has finished being
processed by the container (i.e. Tomcat in my case)?

Ensure that there are no other references to the object and that it has no
wider than request scope. This will not guarantee that the object doesn't
exist, but it should ensure that it isn't reachable.

- Lew
 
T

Tor Iver Wilhelmsen

Dave said:
How are objects instantiated using the above method treated differently from
<jsp:useBean ...> objects?

The variables created in the servlet source from <jsp:useBean> are
method variables in the generated _jsp_service() method. The first
kind are as another poster pointed out put into the source outside of
this method, e.g. you can use it to declare other methods, static
fields, or add instance vars:

<%!

public static final String AFIELD = "aField";

public String getFoo() {
return "Foo";
}

private int someInstanceVar = 42;

%>

<%=AFIELD%> <%=getFoo()%>

Be very careful with instance vars, either in JSPs or servlets, since
unless you have declared it singlethreaded the container can reuse the
object for two (concurrent) calls.
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top