Retrieving data from an ArrayList and presenting them in a JSP

Discussions

Web tier: servlets, JSP, Web frameworks: Retrieving data from an ArrayList and presenting them in a JSP

  1. Dear Fellow Java Developers:

    I am developing a catalogue site that would give the user the option of viewing items on a JSP page. The thing is, a user may request to view a certain item of which there are several varieties, for example "shirts". There may be 20 different varieties, and the catalogue page would present the 20 different shirts on the page. However, I want to give the user the option of either viewing all the shirts on a single page, or view a certain number at a time, and view the other shirts on a second or third JSP page.

    See the following link as an example:

    http://www.eddiebauer.com/eb/cat_default.asp?nv=2|21472|9&tid=&c=&sc=&cm_cg=&lp=n2f

    I am able to retrieve the data from the database, and present all of them on a JSP, however I am not sure how to implement the functionality so that they can be viewed a certain number at a time. So if I want to present say 12 items on a page and the database resultset brings back 30 items, I should be able to present the first 12 items on page1, the next 12 items on page2, and the remaining 6 items on page3. How would I do this? Below is my scriplet code that I use to retrieve information from the ArrayList that I retrieve from my database and present them in their entirety on a single JSP page:
    <table>
    <tr>

    <%String product=request.getParameter("item");
    ArrayList list=aBean.getCatalogueData(product);
    int j=0, n=2;
    for(Iterator i=list.iterator(); i.hasNext(); j++){
    if(j>n){
    out.print("</tr><tr>");
    j=0;
    }

    Integer id=(Integer)i.next();
    String name=(String)i.next();
    String productURL=(String)i.next();
    out.print("a bunch of html with the above variables embedded inside")

    }

    %>
    </tr>
    </table>

    where aBean is an instace of a JavaBean that retrieves the data from the Database.

    I have two ideas, because each iteration of the for loop represents one row from the database, I was thinking of introducing another int variable k that would be used to count all the iterations in the for loop, thus knowing the exact number. Once we had that value, we would then be able to determine if it was greater than or less than or equal to the maximum number of items we wanted to present on the JSP page( in this case 12). Once we had that value would then pass that value along to the next page and the for loop in each subsequent JSP page would continue from where the previous JSP page left off. The other option, would be to create a new ArrayList for each JSP page, where each JSP page would have an ArrayList that held all the items that it would present and that was it. Which approach is best? And more importantly, how would I implement it?

    Just wondering.
    Thanks in advance to all that reply.
    Sincerely;
    Fayyaz
  2. You could put the Iterator in the Session and depending on it's "hasNext()" Method generate a link to the "next" page or not. In your loop on the same page you can count the iterations and stop after a certain number (e.g. 12). If the Iterator still returns "true" when asked for "hasNext()", you have to create a link to the "next" page - in fact the same page which retrieves the saved iterator and continues with the iterations.
       ...
    <%
       String product = request.getParameter("item");
       Iterator allElements = null;
       // if you came here from some product selection page or link:
       if (product != null)
       {
          // initialize the Iterator with the initial set of catalogue data:
          ArrayList list = aBean.getCatalogueData(product);
          allElements = list.iterator();
       }
       else
       {
          allElements = (Iterator) session.getAttribute("itemIterator");
       }
       int numberOfShownElements = 0;
       while (allElements != null && allElements.hasNext() &&
          numberOfShownElements++ < 11)
       {
          // do your output of the element data
       }
       // if you have more elements:
       if (allElements.hasNext())
       {
          // save the current status of the iteration:
          session.setAttribute("itemIterator", allElements);
          // print a "next" link to this page without the "item" request parameter
          ...
       }
       ...
  3. My understanding is you are in the need of implemention paging functionality. Here is an excellent framework available, very easy to implement it into your application. http://jsptags.com/tags/navigation/pager/pager-taglib-2.0.html

    Hope this help !!!
    Senthil.