jsp code:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>page</title>
<script type="text/javascript">
function sub(){
var size = document.getElementById("xx").value;
window.location="<%=path%>/userServlet?size="+size;
}
function to(){
var p = document.getElementById("toindex").value;
window.location="<%=path%>/userServlet?p="+p;
}
</script>
</head>
<body>
<a href="userServlet">分页显示</a>
<center>
<table border="1" cellpadding="10" cellspacing="0">
<tr><td>id</td><td>Name</td></tr>
<c:forEach items="${page.pageDate}" var="pg">
<tr><td>${pg.id}</td><td>${pg.username }</td></tr>
</c:forEach>
</table>
共${page.pageCount }页 第${page.pageNow }页 每页显示${ page.pageSize}条记录
<c:if test="${page.pageNow ne 1}">
<a href="userServlet?p=${page.pageNow-1 }">上一页</a>
</c:if>
<c:if test="${page.pageNow ne page.pageCount}">
<a href="userServlet?p=${page.pageNow+1 }">下一页</a>
</c:if>
每页显示<input type="text" size="3" id="xx"/>条
<input type="button" value="GO" οnclick="sub()"/>
转到第<input type=text id="toindex" size=2/>页 <input type="button" value="GO" οnclick="to()"/>
</center>
</body>
</html>
servlet code:
response.setContentType("text/html;charset=gbk");
UserService us = new UserService();
//当前页
String page = request.getParameter("p");
if(page == null){
page = "1";
}
int p = Integer.parseInt(page);
//每页显示记录条数
String sz = request.getParameter("size");
if(sz == null){
sz = "10";
}
int size = Integer.parseInt(sz);
Page pages = us.findAll(p, size);
request.setAttribute("page", pages);
request.getRequestDispatcher("/index.jsp").forward(request, response);
service code:
public Page findAll(int pageNow,int size){
Page page = new Page();
UserDAO userDAO = new UserDAO();
int begin = (pageNow-1)*size;
int end = begin+size;
int count = userDAO.pageCount();
int pageCount = count%size==0?count/size:count/size+1;
page.setPageNow(pageNow);
page.setPageCount(pageCount);
page.setPageSize(size);
page.setPageDate(userDAO.findAll(begin, end));
return page;
}
DAO(jdbc) code:
public List findAll(int begin,int end){
List list = new ArrayList();
conn = DBUtil.getConnection();
try {
pst = conn.prepareStatement("select * from (select id,username,rownum r from userinfo) where r>? and r<=?");
pst.setInt(1, begin);
pst.setInt(2, end);
rs = pst.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.getClose(rs);
DBUtil.getClose(pst);
DBUtil.getClose(conn);
}
return list;
}
public int pageCount(){
int i = 0;
conn = DBUtil.getConnection();
try {
st = conn.createStatement();
rs = st.executeQuery("select count(*) from userinfo");
if(rs.next()){
i = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.getClose(rs);
DBUtil.getClose(pst);
DBUtil.getClose(conn);
}
return i;
}
封装类page code :
private int pageNow;//当前页
private int pageSize; //每页显示条数
private int pageCount; //总页数
private List pageDate; //数据
可以用构造方法初始化