日期:2014-05-16  浏览次数:20802 次

MySQL实现分页技术
先给出servlet
package com.ourchr.shop.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 com.ourchr.shop.bo.ProductBO;
import com.ourchr.shop.entity.Product;

public class Product_listServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String sPageNow = request.getParameter("pageNow");
int rowCount = new ProductBO().getRowCount();
int pageNow=1;
int pageSize = 5;
int pageCount;
if(sPageNow !=null){
pageNow = Integer.parseInt(sPageNow);
}
List<Product> products = new ProductBO().getAllProducts(pageNow,pageSize);
if(rowCount % pageSize ==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize+1;
}
request.setAttribute("pageNow", pageNow);
request.setAttribute("pageCount", pageCount);
request.setAttribute("pageSize", pageSize);
request.setAttribute("rowCount", rowCount);
request.setAttribute("products",products);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}

}
在给出jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
<title></title>
<link rel="stylesheet" href="../css/drp.css">
<script type="text/javascript" src="js/small_window.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<style type="text/css">
/* CSS Document */
body {
font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica,
sans-serif;
color: #4f6b72;
background: #E6EAE9;
}

a {
color: #c75f3e;
}

#mytable {
width: 100%;
padding: 0;
margin: 0;
}

caption {
padding: 0 0 5px 0;
width: 700px;
font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
text-align: right;
}

th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
}

/*power by www.winshell.cn*/
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}

td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}

/*power by www.winshell.cn*/
td.alt {
background: #F5FAFA;
color: #797268;
}