Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayLists 1

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
hello,
loads of questions, but I am very new to this
:)

I have created a class that stores my establishment details like :

while(rs.next()) {

RS_Name = rs.getString("Establishmentname");
RS_Postcode = rs.getString("Postcode");
RS_County = rs.getString("County");
RS_Telephone = rs.getString("Telephone");


EstablishmentResultBean theBean = new EstablishmentResultBean(RS_Name, RS_Postcode, RS_County, RS_Telephone);

but I'm now trying to pass that result into an Array called establishmentlist, then loop through and add all values. any ideas ?
or is that just silly ?
 
Do you mean ....

Code:
ArrayList establishmentList = new ArrayList();
while(rs.next()) { 
            
         RS_Name = rs.getString("Establishmentname");
         RS_Postcode = rs.getString("Postcode");
         RS_County = rs.getString("County");
         RS_Telephone = rs.getString("Telephone");

            
EstablishmentResultBean theBean = new EstablishmentResultBean(RS_Name, RS_Postcode, RS_County, RS_Telephone);
establishmentList.add(theBean);

Then to get at your establishmentList beans ...

Code:
for (int i = 0; i < establishmentList.size(); i++) {
   EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);p
   System.err.println(erb);
}
 
should be a &quot;}&quot; after the rs.next() while loop, and the &quot;;p&quot; is a typo ...
 
That looks good,
but, this code is in my class.
do I put :

for (int i = 0; i < establishmentList.size(); i++) {
EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);p
System.err.println(erb);
}

in my jsp ?
 
just reading some notes,
do you think using the iteratorTag would be a better way of doing this ?
 
>>>> do you think using the iteratorTag would be a better way of doing this

Well, quite possibly ... it depends if it performs the function you desire.


As for whether or not you put the code -
Code:
for (int i = 0; i < establishmentList.size(); i++) {
   EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);
   System.err.println(erb);
}

in your JSP is up to you - if you want to diplay the data held within the bean to a user - then yes ...

You must remember though that the above loop demonstrates to you how to loop an ArrayList, to access objects contained within. You must decide what to do with that Bean one you have retrieved it, and how and when to display the data. You may want to consider writing a &quot;toString()&quot; method which you could call, to print out all the values of that the bean holds - so for example ::

Code:
for (int i = 0; i < establishmentList.size(); i++) {
   EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);
   out.println(&quot;<table>&quot;);
   out.println(&quot;<tr><td>&quot; +erb.toString() +&quot;</td></tr>&quot;);
   out.println(&quot;</table>&quot;);
}
 
hello,
yeah, I do want to display the data to the users, but I don't really want to embed html within my java class, like your example above.

IF I add the code :
<% for (int i = 0; i < establishmentList.size(); i++) {
EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);


%>
to my jsp page then I get errors saying cannot resolve establishmentList.

I have added :
<jsp:useBean id=&quot;listBean&quot; type=&quot;establishment.Establishment_Search&quot; scope=&quot;request&quot;/>

above the scriplet, but still no joy. ?
what am I missing ?
 
establishmentList is your ArrayList is it not - is it in scope (ie not within some &quot;if&quot; statements so that the rest of the code cannot see it ?
 
correct,
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

ArrayList establishmentList = new ArrayList();

//all the SQL here

while(rs.next()) {

RS_Name = rs.getString(&quot;Establishmentname&quot;);
RS_Postcode = rs.getString(&quot;Postcode&quot;);
RS_County = rs.getString(&quot;County&quot;);
RS_Telephone = rs.getString(&quot;Telephone&quot;);

EstablishmentResultBean theBean = new EstablishmentResultBean(RS_Name, RS_Postcode, RS_County, RS_Telephone);
establishmentList.add(theBean);
}

ServletContext context = getServletContext();

context.getRequestDispatcher(&quot;Establishment_Result.jsp&quot;).forward(req, resp);




 
I think I'm so close to displaying the data !
this would make my day if you can help me sort this out !
in my JSP I am calling the bean like :

<jsp:useBean id=&quot;listBean&quot; type=&quot;establishment.Ed_Establishment_Search&quot; scope=&quot;request&quot; />
<% for (int i = 0; i < listBean.establishmentList.size(); i++) {
establishment.EstablishmentResultBean erb = (establishment.EstablishmentResultBean) listBean.establishmentList.get(i);
%>

<TR border=1>
<TD class=&quot;Est_SearchRes_TD&quot;><%= erb.getEst_Name() %></TD>

there are no errors , but when run I get the error:

bean listBean not found within scope

!! what does it all mean ?
 
I would try to avoid mixing scriptlets and tags - its best to use one or the other, or you'll get in a muddle ...

Try to get your listBean object using session attributes (remember to set the attribute first)

Code:
 Ed_Establishment_Search listBean = (.Ed_Establishment_Search) session.getAttibute(&quot;listBean&quot;);
 
morning sebj,
no errors in the code now just a servlet error when running:
[Servlet Error]-[JSP 1.2 Processor]: java.lang.ClassCastException: java.util.ArrayList

in my Ed_Establiushment_Search class (the servlet), after my while statement to add info to the ArrayList I am setting the session attribute like :

HttpSession session = req.getSession();
session.setAttribute(&quot;listBean&quot;, establishmentList);

//Then forward to the JSP
context.getRequestDispatcher(&quot;ED_Establishment_Result.jsp&quot;).forward(req, resp);

then I'm getting the attribute in my JSP the way you showed me last night.
(I have imported java.util.* into my jsp)

 
sebj, I think, I've finnally sorted it !!!
what I've done is created a new class called EstablishmentListBean with the araylist establishemtList and getter and setter methods.

then aftre my while statement in the Ed_Establishment_Search servlet I have put :
EstablishmentListBean theList = new EstablishmentListBean(establishmentList);

then in my JSP I have changed the code to create a new listBean from the new EstablishmentListBean I have created, I can then use the getEstablishmentList() method. !!
It seems to be working atthe moment.

what do you think of this way ?

thank you so much for all your help, you have really taught me so much !!
later.
 
OK, so your variable &quot;establishmentList&quot; is your ArrayList which holds your beans - so in your JSP to retrieve it from the session context, do
Code:
  ArrayList myJSPestablishmentList = (ArrayList)session.getAttribute(&quot;listBean&quot;);

Remember that here, your ArrayList hold the beans, and when you loop the ArrayList to get the beans and data out, you need to cast them also - eg ...
Code:
for (int i = 0; i < establishmentList.size(); i++) {
   EstablishmentResultBean erb = (EstablishmentResultBean) establishmentList.get(i);
   out.println(&quot;<table>&quot;);
   out.println(&quot;<tr><td>&quot; +erb.toString() +&quot;</td></tr>&quot;);
   out.println(&quot;</table>&quot;);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top