分页技术解析:
第一步:查询出总记录数
select count(*) as c from news; countrecord(总记录数)
第二步:定义出每页的记录数
pagesize=10;
第三步:计算总页数
当前页:nowpage=1;
查询出第一页的记录;
select * from news limit 1,10;//第一页
select * from news limit 10,10;//第二页
//当前页应该从第几条记录开始
(当前页-1)*10
第n页应该是:(nowpage-1)*pagesize
select * from new slimit (nowpage-1)*pagesize,pagesize;
代码实现:
1、首先 创建一个 index.jsp页面,实现页面。
代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<div>
<table border="1px" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items ="${entities}" var="entity">
<tr>
<td>${entity.id}</td>
<td>${entity.title}</td>
<td>${entity.content}</td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<br/>
<div>
<span><a href="${pageContext.request.contextPath }/news.do">首页</a></span>
<span><a href="${pageContext.request.contextPath }/news.do?nowpage=${countPage-1}">上一页</a></span>
<span><a href="${pageContext.request.contextPath }/news.do?nowpage=${countPage+1}">下一页</a></span>
<span><a href="${pageContext.request.contextPath }/news.do?nowpage=${countPage}">末页</a></span>
<span>总记录数:${countRecord},总页数:${countPage},当前页:${nowpage}</span>
</div>
</div>
</body>
</html>
2、创建一个 servlet实现页面程序:
package cn.csdn.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.csdn.web.domin.News;
import cn.csdn.web.service.NewsService;
import cn.csdn.web.service.NewsServiceImpl;
public class NewsListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取当前页
NewsService nService = new NewsServiceImpl();
// 记录数
Integer countRecord = nService.findCountRecord();
// 页数
Integer countPage = nService.findCountPage();
String npage = request.getParameter("nowpage");
int nowpage = 1;
if ("".equals(npage) || npage == null) {
nowpage = 1;
} else {
nowpage = Integer.parseInt(npage);
}
if(nowpage<=1){
nowpage=1;
}
if(nowpage >= countPage){
nowpage = countPage;
}
// 当前页面
List<News> entities = nService.findNowPageInfo(nowpage);
// 转发
request.setAttribute("entities", entities);
request.setAttribute("countRecord", countRecord);
request.setAttribute("countPage", countPage);
request.setAttribute("nowpage", nowpage);
request.getRequestDispatcher("./index.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}