JSP Object lifetime

D

Dave

I've been using ASP for a long time but I am working on my first JSP
project. One thing I've noticed that I'm finding a bit strange is the
lifetime of objects. When I instantiate an object in one of my pages, and I
reload the page again (using different data from the database), it seems to
keep the values from the previously instantiated object despite the fact
I've reloaded the page. This despite the fact that I re-instantiate the
object at the top of the JSP page.

Obviously this is an OO-related feature of JSP, but I would like to know how
to ensure that the objects I use in my application are completely
re-initialised when I reload a page.

I figured that setting the scope attribute to "page" would be the correct
way to do this but it doesn't seem to work.

Any advice appreciated.
 
M

Manish Pandit

Can you paste the code you're using to test?

The JSP, under the hood is a servlet that runs in the servlet container
(tomcat or something of similar nature). All the variables that are
used in the JSP end up in that servlet. Now, based on how you declared
them, they end up at different places in that servlet (local variables
vs. class variables). The container manages the lifecycle of this
class.

-cheers,
Manish
 
D

Dave

Manish Pandit said:
Can you paste the code you're using to test?

The JSP, under the hood is a servlet that runs in the servlet container
(tomcat or something of similar nature). All the variables that are
used in the JSP end up in that servlet. Now, based on how you declared
them, they end up at different places in that servlet (local variables
vs. class variables). The container manages the lifecycle of this
class.

The objID value is retained when reloading the JSP page. This is taken from
thisPage bean and is clearly because thisPage stays alive across page
reloads. The confusing part is that the line:

PageBean thisPage = adminDataModel.getCampaignPageModel();

.... should re-initialise thisPage (it includes returns a "new" PageBean
object) when it's called.


My code is below:

==============


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ include file="include/common.jsp" %>
<%@ include file="include/commonPrivate.jsp" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.dgs.cccm.beans.*" %>
<jsp:useBean class="com.dgs.cccm.cont.admin.AdminDataModel"
id="adminDataModel" scope="page" />
<jsp:useBean class="com.dgs.cccm.cont.validator.AdminValidator"
id="validator" scope="page" />
<jsp:useBean class="com.dgs.cccm.db.DataProxyMSSQL" id="dataProxy"
scope="page" />
<jsp:useBean class="com.dgs.cccm.html.HTMLFormGenerator"
id="htmlFormGenerator" scope="page" />
<jsp:useBean class="com.dgs.cccm.html.HTMLComponentGenerator"
id="htmlComponentGenerator" scope="page" />
<%!
/* Declare page variables */

String pageTitle = "Campaign";
String pageHeader = "Manage Campaign";
String pageSubHeader = "Edit New Campaign";
String pageFile = "campaign.jsp";
int objID = 0;

int pageMode = -1;

Vector invalidFieldNames = null;
Vector invalidFieldMessages = null;

boolean isSubmitted = false;
boolean isFormValid = false;

/* Declare page methods */

boolean getDataFromForm(HttpServletRequest request, PageBean thisPage) {
thisPage.setFieldValue("Campaign_ID",
request.getParameter("Campaign_ID"));
thisPage.setFieldValue("Name", request.getParameter("Name"));

return true;
}

%>
<%
/* Initialising scriptlet */
validator = new AdminValidator();
PageBean thisPage = adminDataModel.getCampaignPageModel();

/* lots of irrelevant code snipped from here */

objID = thisPage.getFieldValue("Campaign_ID")

htmlComponentGenerator.setDataProxy(dataProxy);

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ include file="include/htmlHead.jsp" %>
<body>
<center>
<table width="980" border="0" cellspacing="0" cellpadding="0">
<tr><td><%@ include file="include/header.jsp" %></td></tr>
<tr><td>
<!-- MAIN CONTENT STARTS -->
<h1><%=pageHeader%></h1>
<table border=1 width="100%"><tr><td>
<h2><%=pageSubHeader%> (<%=objID%>)</h2>
</td>
<td width="300" align="right">
<%
if (pageMode == FORM_NEW) {
%>
<a href="javascript: saveForm()">Save</a>
<%
} else if (pageMode == FORM_EDIT) {
%>
<a href="javascript: saveForm()">Save</a>
<%
} else if (pageMode == FORM_VIEW) {
%>
<a href="<%=pageFile %>?id=<%=objID%>&m=1">Edit</a>
<%
}
%>
</td></tr>
</table>
<br/>
<%

htmlComponentGenerator.getHTMLValidationList(invalidFieldMessages, out);
%>
<form name="dataForm" id="dataForm" method="post" action="<%=pageFile %>">
<input type="hidden" name="isSubmitted" id="isSubmitted" value="1" />
<input type="hidden" name="m" id="m" value="<%=pageMode %>" />
<%
Vector groups = thisPage.getGroups();
for (int x=0; x<groups.size(); x++) {
GroupBean thisGroup = (GroupBean)groups.elementAt(x);
htmlFormGenerator.getHTMLGroup(thisGroup, out, pageMode, 1);
}


%>
</form>
<br/><br/>
<%
htmlComponentGenerator.getHTMLPageList(dataProxy.getCampaignPages(objID),
out);
%>

<!-- MAIN CONTENT ENDS -->
</td></tr>
<tr><td><%@ include file="include/footer.jsp" %></td></tr>
</table>
</body>
</html>
 
J

Johnny Storm

Dave said:
The objID value is retained when reloading the JSP page. This is taken
from thisPage bean and is clearly because thisPage stays alive across page
reloads. The confusing part is that the line:

PageBean thisPage = adminDataModel.getCampaignPageModel();

... should re-initialise thisPage (it includes returns a "new" PageBean
object) when it's called.

Hello, Dave.
It seems that your bean 'adminDataModel' itself isn't being reinitialized.
Nothing exactly on your page calls out that is obviously wrong.
My suggestion to you is to write a clearData() method for your bean that you
call each time you want a fresh 'adminDataModel' and see if that helps.

Johnny
 
D

Dave

Johnny Storm said:
Hello, Dave.
It seems that your bean 'adminDataModel' itself isn't being reinitialized.
Nothing exactly on your page calls out that is obviously wrong.
My suggestion to you is to write a clearData() method for your bean that
you call each time you want a fresh 'adminDataModel' and see if that
helps.

Good idea, but it doesn't seem to work.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top