0

I am displaying data from database to show the topics posted by users.I want only ten topics to displayed at one page.As soon as 11 topic comes.Next page button is created.Can anyone suggest me the way to do this in JSP.

   while(iterator.hasNext())
   {
    displaytopicbean bean=iterator.next();
      utitle=bean.getTitle();
      utid=bean.getTid();
      uname=bean.getName();

      count++;

          out.print("<td>");
          out.print("<img src='test.png' width='48' height='48' alt='test'/>");
          out.print("</td>");
          out.print("<td>");
          out.print(utitle);
          out.print("</td>");
          out.print("<td>");  
              out.println("<a href=\"");
              out.println(response.encodeURL ("viewusertopic?id="+utid+"&name="+uname+""));
              out.println("\">View</a>");
          out.print("<td width='10%'>");
          %>
            &nbsp;
          <%
          out.print("</td>");
          i++;

          if(count>1)
          {
              count=0;
              out.println("</tr>");
              out.println("<tr>");
              out.print("<td>");
          %>
             &nbsp;
          <% 
             out.print("</td>");
             out.print("<td>");
          %>
             &nbsp;
          <%
             out.print("</td>");

             out.println("</tr>");
             out.println("<tr>");

             continue;
          }
   }

 %>
ace
  • 6,775
  • 7
  • 38
  • 47
coder
  • 19
  • 1
  • 10

1 Answers1

0

Just check if you're currently on 10th element and that there are more elements. If true, then print the next button and issue a break; to break out the while loop.

if (count == 10 && iterator.hasNext()) {
    // Print next button.
    break;
}

Unrelated to the concrete problem, this is not the best approach. You should query two things from the DB: 1) the row count and 2) only exactly those 10 items the enduser really needs to see. Hauling the entire DB table into Java's memory this way is plain inefficient. For SQL-based pagination with help of JDBC, check this question. Also, doing this all straight in the JSP is not the best approach as well. For some hints, check this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • my friend when trying to post a question gets an error message that Sorry,we are not accepting answers from this account?Can u help me regarding this – coder Apr 02 '11 at 18:33
  • 1
    Probably you've posted posts of extremely poor quality before. Contact team@stackoverflow.com for support. – BalusC Apr 02 '11 at 20:55
  • What should my friend write in that? – coder Apr 03 '11 at 06:16
  • Just about the problem that he's encountering and what he has to do to overcome it. Be careful to write the mail in proper English, not in chat/SMS/childish way. – BalusC Apr 03 '11 at 12:34