数据库中的数据表数据如下:
javaBean: 建立Page类用来存储从数据库查询到的数据信息和当前页面信息。
public class Page {
private int pagenum;//当前页面
private int pagesize;//页面大小
private List<Book> books;//页面数据
public int getPagenum() {
return pagenum;
}
public void setPagenum(int pagenum) {
this.pagenum = pagenum;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
Dao层(只给出了方法):
public List<Book> getPageDates(int startindex, int endindex) {
// TODO Auto-generated method stub
List<Book> bookList=new ArrayList<Book>();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * " +
"from book " +
"limit ?,? ";
con=DbUtils.getConnection();//连接数据库
try {
ps=con.prepareStatement(sql);
ps.setInt(1,startindex);
ps.setInt(2, endindex);
rs=ps.executeQuery();
while(rs.next()) {
Book b1=new Book();
b1.setBookid(rs.getInt(1));
b1.setBookname(rs.getString(2));
b1.setAuthor(rs.getString(3));
b1.setPublishing_house(rs.getString(4));
b1.setPrice(rs.getDouble(5));
b1.setInformation(rs.getString(6));
b1.setStock(rs.getInt(7));
bookList.add(b1);
}
return bookList;
}catch(Exception e) {
return null;
}
}
Service层(只给出了查找页面数据的方法):
public Page getPageDates(int startindex,int endindex) {
Page page=new Page();
page.setBooks(bookDao.getPageDates(startindex, endindex));
return page;
}
Servlet(doGet方法):首先判断当前页面是那一页,若获取当前页面为空或者值等于0,则从数据表0索引位置开始查找;反之,当前页面的大小乘每页数据量为起始索引位置开始查找。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String pagenum=(String)request.getParameter("pagenum");//获取当前页面大小
int startindex=0;
int endindex=0;
Integer page_num;
if(pagenum==null) {//当前页面为空
page_num=0;
startindex=0;
endindex=5;
}else {//当前页面不为空
page_num=Integer.parseInt(pagenum);
if(page_num<=0) {
page_num=0;
startindex=0;
endindex=5;
}else {
startindex=5*page_num;
endindex=5;
}
}
Page page=businessService.getPageDates(startindex, endindex); //查找数据
page.setPagenum(page_num);
request.setAttribute("page", page); //将页面信息作为参数传递给jsp页面
request.getRequestDispatcher("showBookByPage.jsp").forward(request, response);
}
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.bsol.hss.bean.Book" %>
<%@ page import="com.bsol.hss.bean.Page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>书籍信息</title>
</head>
<body>
<%
Page page1=(Page)request.getAttribute("page");//获取从Servlet传递过来的页面信息
List<Book> books=page1.getBooks();//得到页面存储的书籍信息
int pagenum=page1.getPagenum(); //得到当前页面为第几页
%>
<div>
<big>所有书籍信息</big>
</div>
<div >
<table >
<% for(int i=0;i<books.size();i++){
Book book=books.get(i); %>
<tr>
<td>
<%= i+1 %>.<%=book.getBookname() %>
</td>
<td>
库存:<%= book.getStock() %>
</td>
</tr>
<% } %>
</table>
<Button οnclick="window.location.href='ShowBooksByPageServlet?pagenum=<%= pagenum-1%>'">上一页</Button>
<Button οnclick="window.location.href='ShowBooksByPageServlet?pagenum=<%= pagenum+1%>'">下一页</Button>
</div>
</body>
</html>
jsp页面显示如下: