JSP Paging

Hi..
I have developed one web-page which shows ALL employee records fetched from a employee table and display them in my JSP page.
It has contain more than 1000's of records. I need to display first 10 Records in my page. There should be some 4 links below the records display which get me to see next 10 records or previous 10 records or first 10 records or Last 10 records.
IS it possible in JSP?? Can u provide me a code

Questions by ramesh.sure   answers by ramesh.sure

Showing Answers 1 - 3 of 3 Answers

mini2008

  • Jul 14th, 2008
 

Paging In JSP Page:
--------------------------

1. Change Your SP which is going to retrieve data by adding 2 more columns  CurrentPage,DisplayCount and the usual other Parameters
 that u have to pass.
2. while passing Parameters from Front End You have to add these Two Columns also.

3.The Code you have to use to get the Paging in JSP that i mentioned below.

4.In Inner HTML i have given example code that how u have to Pass the page Number ,Number count depending upon your code you can        change it


<%
// Variable declaration 
String gsPageNo=null; //the page no
int ginop=0; // number of pages
int lcpage=0;  //for previous page and next page
String liCurrPage=null; // current page no


// get the page number  from hidden variable
liCurrPage=request.getParameter("pageNo");
  if(liCurrPage==null)
  {
   liCurrPage="1";
  }


// check the submit value
if(gsHdSubmit!=null)   // submit value
  {
   if(gsHdSubmit.equals("prev"))
   {
    lcpage=Integer.parseInt(liCurrPage)-1;
    liCurrPage=lcpage+"";
   }
   else if(gsHdSubmit.equals("next"))
   {
    lcpage=Integer.parseInt(liCurrPage)+1;
    liCurrPage=lcpage+"";
   }
   if(gsHdSubmit.equals("0"))
    liCurrPage="1";
   else //For First Time
   {
    gsPageNo="1";
   }
  }


//while passing it through SP
   gsPageNo="1";    //set the Page Number
   String lstempArray[] = {liCurrPage+"",giDispCount+"",gsDate,gsTrans,gsUserID};  // the parameters for SP that we are going to pass


// retrieve Number of Pages from SP
    ginop=Integer.parseInt(gCRcachedrowset.getString("nop"));
    if (lbifRecords)   // checks If it is First Time You are going to Display
     {
      if(ginop>1)
      {    
%>       <script language="javascript">
        document.all["testInner"].innerHTML="<TABLE><td class=head3><input type=text size=5 name=txtpagenum value='<%=liCurrPage%>'><input type=hidden name=txtClientCode value='<%=gsClientCode%>'>&nbsp;of&nbsp;<%=ginop%>&nbsp;&nbsp;<input type=button class='stylesubmit' name=pgsubmit value=Go onclick='return fnpgsubmit("current")' onchange='fnchange()'>&nbsp;&nbsp;<input type=button class='stylesubmit' name=hdprev value='<<' onclick='return fnpgsubmit("prev")'> <input type=button class='stylesubmit' name=hdnext value='>>' onclick='return fnpgsubmit("next")'></TABLE>";
       </script>
<%      }
%>
      <script language="javascript">
 // Validation for Paging
       function fnpgsubmit(navigate)
       {
        if(navigate=="current")
        {
         if(document.frmName.txtpagenum.value=="")
         {
          alert("Enter Number !!!");
          document.frmName.txtpagenum.focus();
          return false;
         }
         if(isNaN(document.frmName.txtpagenum.value))
         {
          alert("Please Enter Numeric Value Only !!!");
          document.frmName.txtpagenum.value="";
          document.frmName.txtpagenum.focus();
          return false;
         }
         if(eval(document.frmName.txtpagenum.value)<1)
         {
          alert("Please enter page number greater than zero !!");
          document.frmName.txtpagenum.focus();
          return false;
         }
         if(eval(document.frmName.txtpagenum.value)>eval(document.frmName.hidtotal.value))
         {
          alert("Please enter page number less or equal to no of total pages!!!");
          document.frmName.txtpagenum.focus();
          return false;
         }
        }
        if(navigate=="current")
        {
         document.frmName.pageNo.value=document.frmName.txtpagenum.value;
         document.frmName.hdsubmit.value="current";
         document.frmName.submit();
        }
        else if(navigate=="next")
        {
         if(eval(document.frmName.pageNo.value)==document.frmName.hidtotal.value)
         {
          alert("Forward Limit Exceed !!");
          document.frmName.txtpagenum.focus();
          return false;
         }
         document.frmName.pageNo.value=eval(document.frmName.pageNo.value);
         document.frmName.hdsubmit.value="next";
         document.frmName.submit();
        }
        else if(navigate=="prev")
        {
         if(eval(document.frmName.pageNo.value)==1)
         {
          alert("Backward Limit Exceed !!");
          document.frmName.txtpagenum.focus();
          return false;
         }
         document.frmName.pageNo.value=eval(document.frmName.pageNo.value);
         document.frmName.hdsubmit.value="prev";
         document.frmName.submit();
        }
       }
      </script>
// Display the Table Header

      <%

      lbifRecords=false;
     }
%>

// Hidden Variables for number of pages and Current page

</table>
 <input type="hidden" name="hidtotal" value="<%=ginop%>">
 <input type="hidden" name="pageNo" value="<%=liCurrPage%>">
</form>

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions