Link to home
Start Free TrialLog in
Avatar of adeelzzz
adeelzzzFlag for United Arab Emirates

asked on

Get Array List in a JSP Page.

hi every one..
i m going to develop my first application in JSP+Struts. and i got some problem.

i want to display the whole data of my DB Table in a JSP Page.
i get all data through SQL Query and put that in a arrayList.
Here's the code for that

ArrayList allBulletin = null;
HashMap bulletins= null;

while (rset.next()) {

bulletins = new HashMap();
                        
bulletins.put("BULLETIN_TYPE_OID", new Integer(rset.getInt("BULLETIN_TYPE_OID")));
bulletins.put("BULLETIN_TYPE_CD",rset.getString("BULLETIN_TYPE_CD"));
bulletins.put("BULLETIN_TYPE_DESC",rset.getString("BULLETIN_TYPE_DESC"));
bulletins.put("CREATE_BY",rset.getString("CREATE_BY"));
bulletins.put("ACTIVE_IND",rset.getString("ACTIVE_IND"));
bulletins.put("CREATE_DATE",rset.getString("CREATE_DATE"));
                  
allBulletin.add(bulletins);
}//end of while (rset.next())
                  
request.setAttribute("RS", allBulletin);
                  
return("success");

==========================================================
now i m in my JSP and here i try to access data with this ArrayList but it display that in wrong format..
can any body help me abt this issue.

the code ro access ArrayList is Following


<%
ArrayList allBulletin= null;
//To see the Elements of the arraylist
allBulletin = (ArrayList)request.getAttribute("RS");


System.out.println("The size of arraylist ="+ allBulletin.size());
System.out.println("The arraylist is Empty? ="+ allBulletin.isEmpty());

if (!allBulletin.isEmpty()){
      

      for (int i=1 ;i <= allBulletin.size();i++)
      {
      %>
  <tr>
        <td class="style22" width="66" height="17"><%=allBulletin.get(2)%></td>
      <td class="style22" width="213" height="17"><%=allBulletin.get(3)%></td>
      <td class="style22" width="87" height="17"><%=allBulletin.get(4)%></td>
      <td class="style22" width="79" height="17"><%=allBulletin.get(5) %></td>
        <td class="style22" width="65" height="17">
      <%
      if (allBulletin.get(6).equals("A"))
            {out.println("Active");}
            else
            {out.println("In-Active");}
      %>
      </td>
      <td width="41" height="17"><p align="center"><a href="Bulletin_Type.jsp">Edit</p></a></td>

  </tr>
<%
Avatar of WelkinMaze
WelkinMaze

hi,
1) What is the idea of
for (int i=1 ;i <= allBulletin.size();i++)

I don't see to use "i" nowhere in the body of the loop
So you're trying to output several times the same??

2) allBulletin.get(2) - isn't this one that you're trying to print a hashmap?

3) why allBulletin.get(2) to allBulletin.get(6)?
Avatar of Siva Prasanna Kumar
Right now in your above code you are not dieplaying the data instead you are displaying the hashmaps.
i.e
       <td class="style22" width="66" height="17"><%=allBulletin.get(2)%></td>
     <td class="style22" width="213" height="17"><%=allBulletin.get(3)%></td>
     <td class="style22" width="87" height="17"><%=allBulletin.get(4)%></td>
     <td class="style22" width="79" height="17"><%=allBulletin.get(5) %></td>

instead if you want to display data inside the hash map loop through the hashMap using one more for loop.

     for (int i=1 ;i <= allBulletin.size();i++)
     {
for(int j =0;j<allBulletin.get(i).size();;j++)
{
   
// now here you loop through the hashMap and display the data inside it.

In first place, why to keep the results in hashmap? You can directly use the List of java beans to hold the information.
If you need further info, please ask back.
Avatar of adeelzzz

ASKER

well fargo  i got ur point i try to do that in this way and first put  data in HashMap and then pass to Array list and while getting that i gte that array list and pass that to hashmap.
and try to get data through that hash map with .get method.
but now it gave me null pointer exception.
can u put the exact code of this process
regards
ASKER CERTIFIED SOLUTION
Avatar of fargo
fargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS EVERY BODY I DID THAT

<%

ArrayList allBulletin = null;
HashMap bulletin = null;


allBulletin= (ArrayList)request.getAttribute("Bulletins");

//bulletin.get(allBulletin);



System.out.println("IS empty??" + allBulletin.isEmpty());

if (null != allBulletin){
out.println("Total no of Records are=  " + allBulletin.size());
      
for (int i=0 ;i < allBulletin.size();i++)
{
      bulletin = (HashMap)allBulletin.get(i);
%>
<tr>
        <td class="TD.OddRowBG" width="66" height="17"><%=bulletin.get("BULLETIN_TYPE_CD")%></td>
        <td class="TD.OddRowBG" width="66" height="17"><%=bulletin.get("BULLETIN_TYPE_DESC")%></td>
        <td class="TD.OddRowBG" width="66" height="17"><%=bulletin.get("CREATE_Date")%></td>
        <td class="TD.OddRowBG" width="66" height="17"><%=bulletin.get("CREATE_BY")%></td>
        <td class="TD.OddRowBG" width="66" height="17">
        <%  
        if (bulletin.get("ACTIVE_IND").equals('A'))
              out.println("Active");
             else
              out.println("In Active");      %>
        </td>
                                
</tr>
<%
}//end for
}//end if

%>
adeel, you must be happy that the problem is solved. But keep in mind the way it generally handled with struts (which i mentioned). Keep yourself open to change the handling later on...
hI Adeel,
I believe this post deserves a point split with other experts. Why haven't u split the points?
Moreover, why a grade of B? If you have further questions please ask.
adeelzzz you may want to see the byundt's comment at the bottom here:
https://www.experts-exchange.com/questions/21820101/I-cannot-'save-as'-or-open-files-in-Office-2003-This-is-a-new-problem-today.html

Also you may read about grading in the site's help section.