JSP Object lifetime

Discussion in 'Java' started by Dave, Oct 10, 2006.

  1. Dave

    Dave Guest

    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.
     
    Dave, Oct 10, 2006
    #1
    1. Advertising

  2. 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
     
    Manish Pandit, Oct 10, 2006
    #2
    1. Advertising

  3. Dave

    Dave Guest

    "Manish Pandit" <> wrote in message
    news:...
    > 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>
     
    Dave, Oct 10, 2006
    #3
  4. Dave

    Johnny Storm Guest

    "Dave" <> wrote in message
    news:ArTWg.9238$...
    > "Manish Pandit" <> wrote in message
    > news:...
    >> 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.
    >


    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

    >
    > 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>
    >
     
    Johnny Storm, Oct 11, 2006
    #4
  5. Dave

    Dave Guest

    "Johnny Storm" <> wrote in message
    news:xtYWg.3757$9Y1.2730@trndny03...
    > "Dave" <> wrote in message
    > news:ArTWg.9238$...
    >> "Manish Pandit" <> wrote in message
    >> news:...
    >>> 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.
    >>

    >
    > 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.
     
    Dave, Oct 11, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Basterfield

    lifetime of Session object

    Colin Basterfield, Jun 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    440
    Colin Basterfield
    Jun 3, 2004
  2. Replies:
    0
    Views:
    425
  3. =?Utf-8?B?RGlmZmlkZW50?=

    Worker process and Cache object's lifetime

    =?Utf-8?B?RGlmZmlkZW50?=, Feb 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 2, 2006
  4. pt
    Replies:
    8
    Views:
    778
  5. Oliver Tengler

    SWIG: Tie lifetime of object to a result

    Oliver Tengler, Nov 14, 2003, in forum: Python
    Replies:
    0
    Views:
    276
    Oliver Tengler
    Nov 14, 2003
Loading...

Share This Page