1.封装PageBean

import java.util.List;

/**
 * 分页的JavaBean
 * @author Administrator
 */
public class PageBean<T> {
	
	// 当前页
	private int pageCode;
	
	// 总页数
	// private int totalPage;
	
	// 总记录数
	private int totalCount;
	// 每页显示的记录条数
	private int pageSize;
	// 每页显示的数据
	private List<T> beanList;
	
	public int getPageCode() {
		return pageCode;
	}
	public void setPageCode(int pageCode) {
		this.pageCode = pageCode;
	}
	
	/**
	 * 调用getTotalPage() 获取到总页数
	 * JavaBean的属性规定:totalPage是JavaBean是属性 ${pageBean.totalPage}
	 * @return
	 */
	public int getTotalPage() {
		// 计算
		int totalPage = totalCount / pageSize;
		// 说明整除
		if(totalCount % pageSize == 0){
			return totalPage;
		}else{
			return totalPage + 1;
		}
	}
	
	/*public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}*/
	
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List<T> getBeanList() {
		return beanList;
	}
	public void setBeanList(List<T> beanList) {
		this.beanList = beanList;
	}
}

2.Servlet

/**
	 * 获取当前页
	 *如果用户没有传,默认是第一页,如果传了,就是几
	 * @param request
	 * @return
	 */
	public int getPageCode(HttpServletRequest request){
		String pc = request.getParameter("pc");
		// 判断
		if(pc == null || pc.trim().isEmpty()){
			return 1;
		}
		return Integer.parseInt(pc);
	}
/**
	 * 分页查询
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 处理当前页
		 * 处理每页显示的记录条数
		 */
		// 当前页
		int pageCode = getPageCode(request);
		// 处理每页显示的记录条数
		int pageSize = 4;
		try {
			// 调用业务层,分页查询
			PageBean<Product> page = new ProductService().findByPage(pageCode,pageSize);
			// 存入
			request.setAttribute("page", page);
			// 转发
			request.getRequestDispatcher("/jsp/pages.jsp").forward(request, response);
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

3.service

/**
	 * 分页查询
	 * @param pageCode
	 * @param pageSize
	 * @return
	 * @throws SQLException 
	 */
	public PageBean<Product> findByPage(int pageCode, int pageSize) throws SQLException {
		return new ProductDao().findByPage(pageCode,pageSize);
	}

4.dao


/**
	 * 分页查询
	 * 
	 * @param pageCode
	 * @param pageSize
	 * @return
	 * @throws SQLException 
	 */
	public PageBean<Product> findByPage(int pageCode, int pageSize) throws SQLException {
		PageBean<Product> page = new PageBean<>();
		// 属性是空的
		page.setPageCode(pageCode);
		page.setPageSize(pageSize);

		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		// ScalarHandler 处理聚合函数
		long count = (long) qr.query("select count(*) from product", new ScalarHandler());
		// 设置总记录条数
		page.setTotalCount((int) count);

		// limit a,b a = (当前页-1) * b
		List<Product> beanList = qr.query("select * from product limit ?,?", new BeanListHandler<Product>(Product.class),
				(pageCode - 1) * pageSize, pageSize);
		
		// 每页显示的数据
		page.setBeanList(beanList);

		return page;
	}

5.utils

public class JdbcUtils {
	
	// 成员变量,创建了C3P0的连接池(连接池中已经存在连接了...)
	private static final ComboPooledDataSource DATASOURCE = new ComboPooledDataSource();
	
	/**
	 * 返回的是C3P0的连接池
	 * @return
	 */
	public static DataSource getDataSource(){
		return DATASOURCE;
	}
	
	/**
	 * 获取连接,返回连接
	 * @return
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
			// 从连接池中来获取连接,conn 是增强过的连接
			conn = DATASOURCE.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 释放资源
	 * @param stmt
	 * @param conn
	 */
	public static void release(Statement stmt,Connection conn){
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				// 已经变成了归还了...
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 释放资源
	 * @param stmt
	 * @param conn
	 */
	public static void release(ResultSet rs,Statement stmt,Connection conn){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				// 把close()给修改了,原来是销毁连接,现在让方法变成归还连接。
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

6.jsp页面

<table border="1" width="100%">
	<tr>
		<th>序号</th>
		<th>图片</th>
		<th>名称</th>
		<th>市场价格</th>
		<th>商城价格</th>
		<th>商品日期</th>
	</tr>
	
	<c:forEach var="p" items="${ page.beanList }" varStatus="vs">
		<tr align="center">
			<td>${ vs.count }</td>
			<td>
				<img src="${ pageContext.request.contextPath }/${p.pimage}" width="100px" height="100px">
			</td>
			<td>${ p.pname }</td>
			<td>${ p.market_price }</td>
			<td>${ p.shop_price }</td>
			<td>${ p.pdate }</td>
		</tr>
	</c:forEach>
	
	分页条 
	<tr>
		<th colspan="6">
			第${ page.pageCode }页/共${ page.totalPage }页
			<a rel="nofollow" href="${ pageContext.request.contextPath }/findByPage?pc=1">首页</a>
			<c:if test="${ page.pageCode > 1 }">
				<a rel="nofollow" href="${ pageContext.request.contextPath }/findByPage?pc=${page.pageCode - 1}">上一页</a>
			</c:if>
			
	
				begin和end值是变化的,设置begin和end的值
				逻辑:
					* 如果总页数<=10页,让begin=1 end=总页数
					* 如果总页数 > 10页,begin=当前页-5 ,end = 当前页 + 4
						* 头溢出:如果begin<1,出现了头溢出了,让begin=1 end=10
						* 尾溢出:如果end > 总页数,让begin=总页数-9 end=总页数
					
		
			
			<c:choose>
				<c:when test="${ page.totalPage <= 10 }">
					<c:set var="begin" value="1"/>
					<c:set var="end" value="${ page.totalPage }"/>
				</c:when>
				<c:otherwise>
					<c:set var="begin" value="${ page.pageCode - 5 }"/>
					<c:set var="end" value="${ page.pageCode + 4 }"/>
				头溢出的问题 
					<c:if test="${ begin < 1 }">
						<c:set var="begin" value="1"/>
						<c:set var="end" value="10"/>
					</c:if>
					
					<c:if test="${ end > page.totalPage }">
						<c:set var="begin" value="${ page.totalPage - 9 }"/>
						<c:set var="end" value="${ page.totalPage }"/>
					</c:if>
				</c:otherwise>
			</c:choose>
			
			<c:forEach var="i" begin="${ begin }" end="${ end }">
				<a rel="nofollow" href="${ pageContext.request.contextPath }/findByPage?pc=${i}">[${ i }]</a>
			</c:forEach>
			
			<c:if test="${ page.pageCode < page.totalPage }">
				<a rel="nofollow" href="${ pageContext.request.contextPath }/findByPage?pc=${page.pageCode + 1}">下一页</a>
			</c:if>
			<a rel="nofollow" href="${ pageContext.request.contextPath }/findByPage?pc=${page.totalPage}">尾页</a>
		</th>
	</tr>
</table>