btretzanor
asked on
Paging a JSP page source code -required URGENT-
Paging a JSP page source code -required URGENT-
I need to paging some JSP pages in 20 item.. I DONT KNOW HOW!
i need a source code as example!
I need to paging some JSP pages in 20 item.. I DONT KNOW HOW!
i need a source code as example!
// in your myServlet:
int NUM_PER_PAGE = 20;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// A vector of BasicClientInfoBean objects.
Vector vClients = new Vector();
// Page numbering.
int page = 1;
try{
page = Integer.parseInt(request.g etParamete r("page")) ;
}catch(Exception e){
// default to 1.
}
try{
sql ="select ....";
// TODO: get DB connection:
con = getconnection...;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(makeSQL( ));
int count=0;
while(rs.next() ){
count ++;
//add those client that are on page'th page:
if(((page - 1)* NUM_PER_PAGE + 1 <= count) && (count <= page * NUM_PER_PAGE)){
BasicClientInfoBean client = new BasicClientInfoBean();
client.setFirstName(rs.get String("Fi rstName")) ;
client.setLastName(rs.getS tring("Las tName"));
vClients.add(client);
}
}
rs.close();
stmt.close();
vClients
}catch(SQLException e){
System.err.println("GroupS ervlet ->searchGroup()-> error:" + e.getMessage());
}
// Get the number of pages.
int total = vClients.size();
int totalPages = (int)Math.ceil((double)tot al/NUM_PER _PAGE);
// Set request objects for use in the result screen.
request.setAttribute("clie nts", vClients);
request.setAttribute("page ", "" + page);
request.setAttribute("tota lPages", Integer.toString(totalPage s));
request.setAttribute("tota lCount", Integer.toString(total));
}
int NUM_PER_PAGE = 20;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// A vector of BasicClientInfoBean objects.
Vector vClients = new Vector();
// Page numbering.
int page = 1;
try{
page = Integer.parseInt(request.g
}catch(Exception e){
// default to 1.
}
try{
sql ="select ....";
// TODO: get DB connection:
con = getconnection...;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(makeSQL(
int count=0;
while(rs.next() ){
count ++;
//add those client that are on page'th page:
if(((page - 1)* NUM_PER_PAGE + 1 <= count) && (count <= page * NUM_PER_PAGE)){
BasicClientInfoBean client = new BasicClientInfoBean();
client.setFirstName(rs.get
client.setLastName(rs.getS
vClients.add(client);
}
}
rs.close();
stmt.close();
vClients
}catch(SQLException e){
System.err.println("GroupS
}
// Get the number of pages.
int total = vClients.size();
int totalPages = (int)Math.ceil((double)tot
// Set request objects for use in the result screen.
request.setAttribute("clie
request.setAttribute("page
request.setAttribute("tota
request.setAttribute("tota
}
above code is not pretended to be working, but
it is extracted from my working code and
intended to give you the basic idea.
Hope it helps.
it is extracted from my working code and
intended to give you the basic idea.
Hope it helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Rajendra,
I face this error when i view this page in the browser. It says
Error
-----
"[05/Jul/2001:13:50:36] warning (26668): JSP compilation error: java.lang.Excepti
on: JSP parse error (line 6) - USEBEAN tag is invalid, stack: java.lang.Exceptio
n: JSP parse error (line 6) - USEBEAN tag is invalid"
-------------------------- ---------- ---------- ---------- ---
What's wrong with the bean? What is the iRows for? In your while statement you specify that the iRows must be > than the iRecordsNumber but you initiate the iRows = 0 at the beginning and it was never incremented.
Please help.
Thanks
Jason (clkong@apis.dhl.com)
U can email meif u wish to
I face this error when i view this page in the browser. It says
Error
-----
"[05/Jul/2001:13:50:36] warning (26668): JSP compilation error: java.lang.Excepti
on: JSP parse error (line 6) - USEBEAN tag is invalid, stack: java.lang.Exceptio
n: JSP parse error (line 6) - USEBEAN tag is invalid"
--------------------------
What's wrong with the bean? What is the iRows for? In your while statement you specify that the iRows must be > than the iRecordsNumber but you initiate the iRows = 0 at the beginning and it was never incremented.
Please help.
Thanks
Jason (clkong@apis.dhl.com)
U can email meif u wish to
jason,
did you try mine?
did you try mine?
bagi,
Nope, I have not try yours. But I actually manage to modified rajendra's code without using the bean and it works.
Cheers
Jason
Nope, I have not try yours. But I actually manage to modified rajendra's code without using the bean and it works.
Cheers
Jason
Force/accepted by
Netminder
Community Support Moderator
Experts Exchange
jason/btrezanor:
Having two accounts is a specific violation of the Membership agreement, and the fact that you abandoned a number of questions in one account and have a number of abandoned questions in the second makes the offense more egregious. You will be contacted by Admin.
Netminder
Community Support Moderator
Experts Exchange
jason/btrezanor:
Having two accounts is a specific violation of the Membership agreement, and the fact that you abandoned a number of questions in one account and have a number of abandoned questions in the second makes the offense more egregious. You will be contacted by Admin.
<%
Vector vClients = (Vector)request.getAttribu
int pageNo = Integer.parseInt((String)r
int totalPages = Integer.parseInt((String)r
int totalCount = Integer.parseInt((String)r
boolean hasMorePage = (pageNo < totalPages);
%>
<FORM name="FORM1" method=post action="/servlets/myServle
<% //-- To hold paging logic -- %>
<input type=hidden name="page" value="1">
<center><FONT color=black face=Arial size=2>Search returned <b><%= totalCount %></b> results<% if(vClients.size() != 0) { %> (Page <b><%= pageNo %></b> of <b><%= totalPages %></b>)<%}%></FONT><center
// display l20 customers from vClients:
<% //--------------------- Table Data -----------------%>
<%
// Display message if there were no matches.
if(vClients.isEmpty()){
out.println("<TR bgColor=lightgoldenrodyell
" <TD colspan=8><b><FONT face=Arial size=2>NO CLIENTS MATCHED YOUR SEARCH CRITERIA.</FONT></b></TD>"
}
int row=1;
Iterator it = vClients.iterator();
while(it.hasNext()){
cInfo = (BasicClientInfoBean)it.ne
%>
<TR bgColor=<% if ( (row%2) == 1 ) {%>lightgoldenrodyellow
<%} else {%>aliceblue <%}%> >
<TD><jsp:getProperty name="cInfo" property="firstname" /> <jsp:getProperty name="cInfo" property="lastname" />
</TD></TR><%
row++;
} // end of while loop
%><TR>
//display nav bar at the bottom as:
<% if(pageNo>1) { %>
<b><FONT face=Arial size=2 color=white>
<a href="javascript:onclick=h
Previous Page</a></FONT></b>
<%}%>
<% if(hasMorePage) { %>
<% if(pageNo>1) {%>
<b>|</b>
<%}%>
<FONT face=Arial size=2 color=white><b>
<a href="javascript:onclick=n
Next Page</a></b></FONT>
<%}%>
</FORM>
// where nextPage () is javascript on the page:
function nextPage(page){
document.FORM1.cmd.value="
document.FORM1.page.value=
document.FORM1.submit();
}