#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    Unhappy how to display 50 rows per page using servlet!!URGENT


    hello,
    i am novice servlet programmer.

    my requirement is i want to fetch records from database and then display them .

    and 50 records per page should be displayed and NEXT and PREVIOUS button should be there on each page.

    please if anyone can give sample code then it will be gr8 help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    What database are you using?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    11
    JSP code would look something like this.
    Code:
    TreeMap rows = (TreeMap)session.getTreeMap( "rows" ); // This is a collection of the rows we want to display
    String loopStartStr = request.getParameter( "loopStart" ); // This gets the loop start value from request 
    int loopStart = 0;
    int maxRecords = 50; // max number of records to display
    int loopEnd = maxRecords;
    
    /**
     *  validate the start value and setup an int to work with, if the
     *  string is null or empty use the value of 0, because its the first
     *  time through the results.
     */
    if ( loopStartStr != null && !"".equals(loopStartStr ) ) {
    	loopStart = Integer.parseInt(loopStartStr);
    }
    
    /**
     *  If the rows left to display are less than maxRecords then only loop enough
     *  times to get through the remaining records.  This avoids a null pointer, where
     *  the loop would go beyond the number of records in the TreeMap.
     */
    int rowsLeft = rows.size() - loopStart;
    if (maxRecords > rowsLeft) {
    	loopEnd = rowsLeft;
    }
    
    for (int 	i = loopStart; i < loopEnd; i++ ) {
    	// output recrods here
    }
    
    int newLoopStart = loopStart + loopEnd;
    Then at the bottom of the page...

    Code:
    <FORM name="nextResultsForm" action="">
      <INPUT type="hidden" name="loopStart" value="<%=newloopStart%>"
      <BUTTON name="Next" onclick="document.nextResultsForm.submit();">
    </FORM>
    There may be some syntax errors but it should help you get started.

IMN logo majestic logo threadwatch logo seochat tools logo