tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tim Moore" <tmo...@blackboard.com>
Subject RE: Java Bean Scope questions (a lengthy one) [getting desperate]
Date Thu, 09 Jan 2003 21:06:06 GMT
The problem is that sindex is never reset.  The first time you load the
form page, it iterates through the list of states, but on each
subsequent time, it remains at the end of the list, so hasMoreStates
always returns false.  The reason you didn't see this before is because
a new FormBean instance was created each time you visited the page.  Now
it's accessing the same instance from the session each time.

-- 
Tim Moore / Blackboard Inc. / Software Engineer
1899 L Street, NW / 5th Floor / Washington, DC 20036
Phone 202-463-4860 ext. 258 / Fax 202-463-4863


> -----Original Message-----
> From: Denise Mangano [mailto:DeniseM@complusdata.com] 
> Sent: Thursday, January 09, 2003 4:00 PM
> To: 'Tomcat Users List'
> Subject: RE: Java Bean Scope questions (a lengthy one) 
> [getting desperate]
> 
> 
> Ok, I'm sorry for beating a dead horse, but I am completely 
> at a loss and I
> am starting to get concerned that I won't get this to work.
> 
> As suggested I changed the scope of my bean to session.  This 
> caused some
> problems that if not resolved my webapp will not work.  The 
> code that I use
> for setting a select field to the display the selected option if it is
> valid, or repopulating the list no longer works since 
> changing the scope to
> session.  This has caused me to end up in a never ending loop 
> for the form
> Retry because these select fields are never valid.  For 
> example one of my
> select fields is for the user to select their state.  
> 
> My HTML form  (served through Tomcat although all other HTML pages are
> served through Apache) uses the long hand <select> tag where 
> I list each
> state individually.  My bean contains an array of state 
> abbreviations and
> state names ( all one array in the format "NY","New York","MA",
> "Massachussetts","AL","Alabama"...).  In Retry.jsp I have the 
> following
> code:
> <jsp:useBean name="formBean" class="com.complusdata.beans.FormBean"
> scope="session"/>
> ...........
> <select name="state" size="1">
> <%    if (!formBean.hasStateError()) 
>           { %>
>                  <option value="<%=formBean.getState()%>"
> selected><%=formBean.getStateName()%></option> 
> <%    }  
>           else  {  %>
>                  <option value="" selected><b>--Please select your
> state--</b></option>
> <%                 while(formBean.hasMoreStates())  
>                        {  %> 
>                    <option value=<%= formBean.getStateList()%>><%=
> formBean.getStateList()%></option> 
> <%                 }    
>            }  %>
>             </select>
> ..........
> 
> Relevant methods in FormBean:
> public boolean hasStateError()
>   {
>     if (state.equals(""))
>        return true;
>     else
>        return false;
>   }
> 
>   public boolean hasMoreStates()
>   {
>         if (sindex < stateArray.length)
>                 return true;
>         else
>                 return false;
>   }
> public String getState()
>   {
>         return state;
>   }
> 
> public String getStateName()
>   {
>      int z;
>      for (z=0; z<stateArray.length-1; z=z+2)
>      {
>        if (stateArray[z].equals(state))
>           return stateArray[z+1];
>      }
>      return state;
>  }
> 
> //int sindex=-1; declared at beginning of class
> public String getStateList()
>   {
>               	sindex++;
>   	String stemp = stateArray[sindex];
> 	return stemp;
>   }
> 
> My form calls CCProcess which instantiates an instance of 
> FormBean which
> attempts to validate the form. Now since I changed the scope if I test
> leaving the select list without a state selected, this causes 
> a problem.
> After the first retry, my select lists are populated.  Any 
> time after that,
> it defaults to the "Please select your State" but it does not 
> populate the
> list - it is taking this as the "State" which is obviously 
> not valid, and
> reason for the loop. I also ran a test where I left the state 
> blank of the
> first try, and the select list was populated.  I selected a 
> state then hit
> submit again (with other errors on the form) and the bean did 
> not save the
> state of my selection and the list did not populate.
> 
> If anyone can offer any help I would appreciate it.  Perhaps 
> give me a clue
> if there is something wrong with my code, or maybe some code 
> someone is
> currently using to serve the same purpose.  The thing is, I 
> really don't
> think its my code because it does work when the scope of the bean is
> request.
> 
> Thanks in advance.
> 
> Denise Mangano
> Help Desk Analyst
> Complus Data Innovations, Inc.
> 
> 
> -----Original Message-----
> From: Denise Mangano [mailto:DeniseM@complusdata.com] 
> Sent: Wednesday, January 08, 2003 2:29 PM
> To: 'Tomcat Users List'
> Subject: RE: Java Bean Scope questions (a lengthy one)
> 
> 
> Bill,
> 
> I'm not sure what you mean.  The transaction Bean gets 
> instantiated when I
> call CCSubmit.jsp.  That is the first time it is "mentioned" 
> and CCSubmit is
> only called once from Verify.jsp.  Is what you are saying 
> effectively the
> same as:
> 
> <jsp:useBean id="transaction" class = 
> "com.complusdata.beans.Transaction"
> scope="session" >
>   <jsp:setProperty name="transaction" property="email"
> value="<%=formHandler.getEmail()%>"/>
>    ....
> </jsp:useBean>
> 
> Thanks.
> 
> Denise Mangano
> Help Desk Analyst
> Complus Data Innovations, Inc.
> 
> 
> -----Original Message-----
> From: Bill Barker [mailto:wbarker@wilshire.com] 
> Sent: Wednesday, January 08, 2003 1:42 AM
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Java Bean Scope questions (a lengthy one)
> 
> 
> 
> "Denise Mangano" <DeniseM@complusdata.com> wrote in message
> news:5D83C44941AFD4118B6F0002B302984F438636@EXCHANGE_SERVER...
> > Wow someone read all of that!! ;)
> >
> > For Question #1:  I should have mentioned this before... I tried to 
> > use session scope at one point (don't recall the exact reason but I 
> > thought of it as an attempt to resolve a different 
> problem).  When I 
> > set the scope to session, it caused problems with my form 
> validation.
> > Currently if the
> form
> > is not validated, the user is brought back to Retry.jsp and error 
> > messages appear next to the offending input field.  When I used 
> > session scope, Retry.jsp would be reloaded, but the error messages 
> > would not be displayed... Any thoughts on that?  I am going 
> to go back 
> > and attempt this again now that everything is working as it 
> should up 
> > to Verify.jsp and see if I stand corrected...
> 
> You need:
> <jsp:useBean id="transaction" class = 
> "com.complusdata.beans.Transaction"
> scope="session" />
>   <jsp:setProperty name="transaction" property="email"
> value="<%=formHandler.getEmail()%>"/>
> ....
> 
> 
> Note the final '/>' on jsp:useBean.  The way that you had it 
> originally, the
> properties will only be set if the JSP page needs to create a 
> new instance
> of "transaction".  If it finds one in the session already, 
> the setters are
> skipped.
> 
> 
> 
> >
> > For Question #2 - that's exactly what I needed to know!!
> >
> > For Question #3 - hopefully I can get the session scope to 
> work, but 
> > then how would I program that button?  Would I just put 
> > onClick="Retry.jsp" ?
> >
> > Tim, thank you so much for taking the time!!
> > Denise :)
> >
> > -----Original Message-----
> > From: Tim Moore [mailto:tmoore@blackboard.com]
> > Sent: Tuesday, January 07, 2003 9:24 PM
> > To: Tomcat Users List
> > Subject: RE: Java Bean Scope questions (a lengthy one)
> >
> >
> > > -----Original Message-----
> > > From: Denise Mangano [mailto:DeniseM@complusdata.com]
> > > Sent: Tuesday, January 07, 2003 9:19 PM
> > > To: 'Tomcat Users List'
> > > Subject: Java Bean Scope questions (a lengthy one)
> > >
> > >
> > > Question #1: Why does CCSubmit.jsp not have access to formBean 
> > > properties. I am thinking this is because technically by 
> submitting 
> > > a form with an action of "CCSubmit.jsp" control isn't being 
> > > forwarded to CCSubmit.jsp so the instance of formBean isn't 
> > > forwarded.  The scope of formBean is request. Is that the correct 
> > > scope to use?  **I need to have access to formBean 
> properties from 
> > > the moment it is instantiated to the moment the application has 
> > > completed, but it has to be unique to each user of the
> > > webapp.**   How
> > > could I accomplish this?
> >
> > That's exactly what session scope is for. :-) Request scope 
> means that 
> > the bean is gone at the end of the request (that is, when 
> Verify.jsp 
> > finishes rendering).  If you put it in session scope you'll be set.
> >
> > > Question #2: Is it possible to use two beans within a single jsp 
> > > page?  I would imagine that it is, and if so I believe 
> once I gain 
> > > access to formBean properties, my transaction properties will no 
> > > longer be null. (I included some code below... is this legal?)
> >
> > Absolutely!  As long as they have different id attributes, 
> you can use 
> > as many beans as you like.
> >
> > > Question #3:  I include a button to give the option to go 
> back and 
> > > make changes.  If the user should desire to go back and make 
> > > changes, control should be forwarded to Retry.jsp on button click.
> > > I need to program this button in a way so that when Retry.jsp is 
> > > recalled it will still have access to formBean 
> properties.How do I 
> > > program this? My last attempt using <jsp:forward> cause 
> Retry.jsp to 
> > > load automatically when Verify.jsp was loaded (sorry 
> again Noel ;) 
> > > ).
> >
> > As long as the bean is in session scope, you'll be able to 
> access it 
> > from any page the user accesses.
> > --
> > Tim Moore / Blackboard Inc. / Software Engineer
> > 1899 L Street, NW / 5th Floor / Washington, DC 20036
> > Phone 202-463-4860 ext. 258 / Fax 202-463-4863
> >
> > --
> > To unsubscribe, e-mail: 
> > <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:tomcat-user-help@jakarta.apache.org>
> 
> 
> 
> 
> --
> To unsubscribe, e-mail: 
> <mailto:tomcat-user-> unsubscribe@jakarta.apache.org>
> For 
> additional commands, 
> e-mail:
> <mailto:tomcat-user-help@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:
> <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:tomcat-user-help@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:   
> <mailto:tomcat-user-> unsubscribe@jakarta.apache.org>
> For 
> additional commands, 
> e-mail: <mailto:tomcat-user-help@jakarta.apache.org>
> 
> 

--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message