aspose file tools*
The moose likes Servlets and the fly likes Display results with next and previous buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display results with next and previous buttons " Watch "Display results with next and previous buttons " New topic
Author

Display results with next and previous buttons

smitha rai
Greenhorn

Joined: Jun 29, 2001
Posts: 18
I have an arraylist which contains data fetched from database. I want to display all these results in a jsp in a shot of 10 results at a time. The jsp contains two buttons namely, next and previous. Can any one help me out with a code or some tips. It is quite urgent for me please.......
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
next and previous button call the same servlet implementing following logic:
if (next button)
{
code
}
if(previous button)
{
code
}
when next button is pressed, check the number(id) of last element in previous screen.
when previous button is pressed, check the id of 1st item in the next screen.
hope it helps..
Aman Rustogi
Greenhorn

Joined: May 11, 2001
Posts: 22
Smita i've the same functionality in my code. At a time i'm displaying 20 records resulting from search operation and i'm providing user with 'prev' and 'next' button as well as counter numbers which display next 20 records .. for eg. '1' displays 1-20 records ... '2' displays '21-40' records and so on.
I've have dis working in a servlet.
Do u think by providing u the sample code will solve ur purpose?
and besides i'm not storing the records fetched in sum collection object as the records fetched in my case can vary from anything between 10,000 - 15,000 records a time. So i'm a bit scared to use Collections Object as i'll need to store it in a Session to provide the above functionality.
kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
Originally posted by smitha rai:
I have an arraylist which contains data fetched from database. I want to display all these results in a jsp in a shot of 10 results at a time. The jsp contains two buttons namely, next and previous. Can any one help me out with a code or some tips. It is quite urgent for me please.......

hi smita,
i am pasting code here;i think it would solve ur problem;
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%
String b1=null;
String b2=null;
String b=null;
int i2=0;
int j2=0;
int i3=0;
int j3=5;
int count1=0;
String i1=null;
String j1=null;
String count=null;
try
{
b1= request.getParameter("Next");
b2= request.getParameter("Previous");
i1= request.getParameterValues("i1")[0];
j1= request.getParameterValues("j1")[0];
i2 =Integer.parseInt(i1);
j2 =Integer.parseInt(j1);

count=request.getParameterValues("count")[0];
count1=Integer.parseInt(count);
}
catch(NullPointerException ne) {}
%>
<form method="post" action="http://localhost:8080/examples/jsp/Next.jsp" name="frm" id="frm">

<%
ResultSet rs,rs1=null;
String name=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("Jdbc:Odbc:chaitu");
Statement stmt=con.createStatement();
count1++;
Vector v =new Vector();
Vector v1 = new Vector();

try
{
rs1 = stmt.executeQuery("select deptno,deptname from emp");
while(rs1.next())
{
v.addElement(rs1.getString("deptno"));
v1.addElement(rs1.getString("deptname"));
}
//String v1= (String) v.firstElement();
// System.out.println("firstelement="+v1);
// int k=v.lastIndexOf(v,1);

}
catch(SQLException se) {se.printStackTrace();}
catch(Exception e1){ System.out.println(e1+"guikgg");}

%>

<%

try
{ %>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFDFBF" >

<% if((b == null) && (count1 == 1))
{
System.out.println("in null part it is going");%>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i3+1%>-<%=j3 %> out of <%=v.size()%></font></td></tr></table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<% for(;i3<j3;i3++)
{%>

<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" >
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i3)%></b></font> </td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i3)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>

<%}%>

<% if(v.size()>5)
{
%>
<tr><td align="center"> <input type="submit" name="Next" value="Next"></td></tr>
<input type="hidden" name="i1" value=<%=i3 %>>
<input type="hidden" name="j1" value=<%=j3 %>>
<input type="hidden" name="count" value=<%=count1 %>>
</table>
<%}


} %>


<%}catch(Exception e){}
%>




<%

try
{
if(b1.equals("Next"))
{
j2=j2+5;

if((j2 == v.size()) | | (j2 > v.size()))
{
int l2=i2+5;
%>
<input type="hidden" name="i1" value=<%=l2 %>>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=v.size() %> out of <%=v.size()%></font></td></tr>
</table>
<%
for(;i2<v.size();i2++)
{%>

<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" bordercolor="#00FF00" border=0>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%></b></font> </td>
<td> <font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v1.elementAt(i2)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>


<%}%>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr><td align="center">
<input type="submit" name="Previous" value="Previous"></td></tr>
</table>
<%}

if(j2 < v.size())
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD">
<% for(;i2<j2;i2++)
{%>

<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%> </b></font></td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i2)%></b></font></td></tr>
<%}
%>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="submit" name="Next" value="Next"></td>
<td> <input type="submit" name="Previous" value="Previous"></td>
</table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%
}%>

<%}

}
catch(Exception e){}
%>


<%
try
{

if(b2.equals("Previous"))
{
i2=i2-10;
j2=j2-5;
if(j2 == 5)
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>

<% for(;i2<j2;i2++)
{
%>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" bordercolor="#00FF00" border=0>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%></b></font> </td>
<td> <font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v1.elementAt(i2)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<%}
%>

<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<input type="submit" name="Next" value="Next"></td></tr></table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%
}

if((j2 < v.size()) && (j2 != 5))
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD">
<% for(;i2<j2;i2++)
{%>

<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%> </b></font></td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i2)%></b></font></td></tr>
<%}
%>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="submit" name="Next" value="Next"></td>
<td> <input type="submit" name="Previous" value="Previous"></td>
</table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%}

}

}
catch(Exception e){}
%>

</form>
</body>
</html>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display results with next and previous buttons