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>
<%
LVL 1
adeelzzzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)?
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.

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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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
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.




Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

%>
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...
fargoCommented:
hI Adeel,
I believe this post deserves a point split with other experts. Why haven't u split the points?
fargoCommented:
Moreover, why a grade of B? If you have further questions please ask.
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.