• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15883
  • Last Modified:

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>
<%
0
adeelzzz
Asked:
adeelzzz
  • 5
  • 2
  • 2
  • +1
1 Solution
 
WelkinMazeCommented:
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)?
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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.

0
 
fargoCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
adeelzzzAuthor Commented:
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
0
 
fargoCommented:
First create a bean like follows

public class ResultBean{

  private String bulletinTypeOid;
  privatre String bulletingTypeCd;
 ...// add the same for others like create date etc.
...
....// add the get and set methods for the above mentioned attributes
}

In the place where u are adding the entries to hashmap...do the following

ArrayList allBulletin = null;
ResultBean bean = null
while (rset.next()) {

bean = new ResultBean();
..... // same with others..do take care of casting
bean.setBulletinTypeCd(rset.getString("BULLETIN_TYPE_CD"));                    
bean.setBulletinTypeDesc(rset.getString("BULLETIN_TYPE_DESC"));                    
..... // same with others..do take care of casting
               
allBulletin.add(bean);
}//end of while (rset.next())
               
request.setAttribute("RS", allBulletin);

Now that we have the list of beans. Let's access them in jsp

        <logic:present name="RS">
          <logic:notEmpty name="RS">
            <logic:iterate id="element" name="RS" >
                 <tr>
                  <td><bean:write name="element" property="bulletinTypeOid" filter="true"/></td>
                 <td> <bean:write name="element" property="bulletinTypeCd" filter="true"/></td>
                   ............// same for others
                 </tr>
            </logic:iterate>
          </logic:notEmpty>
        </logic:present>

This is neat and clean way of handling. Further u should also consider to make Action classes not handling the database handling logic. But for the first step..make the above work.




0
 
adeelzzzAuthor Commented:
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

%>
0
 
fargoCommented:
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...
0
 
fargoCommented:
hI Adeel,
I believe this post deserves a point split with other experts. Why haven't u split the points?
0
 
fargoCommented:
Moreover, why a grade of B? If you have further questions please ask.
0
 
WelkinMazeCommented:
adeelzzz you may want to see the byundt's comment at the bottom here:
http://www.experts-exchange.com/Applications/MS_Office/Q_21820101.html

Also you may read about grading in the site's help section.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now