1、servlet
String pageSize = request.getParameter("pageSize");//从 <select name="pageSize">表单中获得
String pageNo = request.getParameter("pageNo"); //从<select name="pageNo">表单中获得
if(pageSize == null) {
pageSize = "10";
}
if(pageNo == null) {
pageNo = "1";
}
request.setAttribute("pageSize", pageSize);
request.setAttribute("pageNo", pageNo);
调用list(request, pageSize,pageNo)
转发至遍历页面request.getRequestDispatcher("index.jsp").forward(request,response);
2、list(HttpServletRequest request, String strpageSize,String strpageNo)
在request中插入如下参数
int pageSize = Integer.parseInt(strpageSize); //每页记录数
int pageNo = Integer.parseInt(strpageNo); //当前页号
int start = pageSize * (pageNo - 1); //当前页首记录号
int rowCount = count.size(); //总记录数
int pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : (rowCount / pageSize) + 1;//总页数
int pageFirstNo = 1; //首页
int pageLastNo = pageCount; //尾页
int pagePreNo = pageNo > 1 ? pageNo - 1 : pageNo; //前一页
int pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount; //后一页
String sql2 = "select *from book limit " + start + "," + pageSize;//查询出请求的行
3、index.jsp
在form表单中可以实现如下功能
下拉列表:可选当前页数
<select name="pageNo" onchange="document.all.form1.submit();">
<%
int pageCount = (Integer) request.getAttribute("pageCount");
%>
<%
for (int i = 1; i <= pageCount; i++) {
%>
<option value="<%=i%>"
<%
if(pageNo.equals(i+"")){
%>
selected="selected"
<%
}
%>
><%=i%></option>
<%
}
%>
</select>
下拉列表:可选每页记录数
<select name="pageSize" onchange="document.all.pageNo.value='1';
document.all.form1.submit();">
//document.all.pageNo.value='1'将所选项改为1
<option value="10" <%if(pageSize.equals("10")){%>
selected="selected" <%}%>>10</option>
<option value="25" <%if(pageSize.equals("25")){%>
selected="selected" <%}%>>25</option>
<option value="50" <%if(pageSize.equals("50")){%>
selected="selected" <%}%>>50</option>
<option value="100" <%if(pageSize.equals("100")){%>
selected="selected" <%}%>>100</option>
<option value="200" <%if(pageSize.equals("200")){%>
selected="selected" <%}%>>200</option>
<option value="300" <%if(pageSize.equals("300")){%>
selected="selected" <%}%>>300</option>
<option value="500" <%if(pageSize.equals("500")){%>
selected="selected" <%}%>>500</option>
</select>
首页、前一页、后一页、尾页
<TD align="center">总记录数:<%=request.getAttribute("rowCount")%></TD>
<TD align="right"><a
href="javascript:document.all.pageNo.value='<%=request.getAttribute("pageFirstNo") %>';document.all.form1.submit();">首页</a>
<a
href="javascript:document.all.pageNo.value='<%=request.getAttribute("pagePreNo") %>';document.all.form1.submit();">前一页</a>
<a
href="javascript:document.all.pageNo.value='<%=request.getAttribute("pageNextNo") %>';document.all.form1.submit();">后一页</a>
<a
href="javascript:document.all.pageNo.value='<%=request.getAttribute("pageLastNo") %>';document.all.form1.submit();">尾页</a>