4.创建FindServlet类
doGet()方法对分页请求进行处理。
package form; import java.io.*; import com.BookDao; import com.BookBean; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class FindServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int currPage=1; if(req.getParameter("page")!=null){ currPage=Integer.parseInt(req.getParameter("page")); } BookDao dao=new BookDao(); List<BookBean> list=dao.find(currPage); req.setAttribute("list", list); int pages; int count=dao.findCount(); if(count%BookBean.PAGE_SIZE==0){ pages=count/BookBean.PAGE_SIZE; } else{pages=count/BookBean.PAGE_SIZE+1;} StringBuffer sb=new StringBuffer(); for(int i=1;i<=pages;i++){ if(i==currPage){ sb.append("["+i+"]"); }else{ sb.append("<a href='FindServlet?page="+i+"'>"+i+"</a>"); } sb.append(" "); } req.setAttribute("bar",sb.toString()); req.getRequestDispatcher("bookList.jsp").forward(req,resp); }
5.编写bookList.jsp页面
bookList.jsp获取图书信息并分页显示。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*" %> <%@ page import="com.BookBean" %> <% // 获取图书信息集合 List<BookBean> list = (List<BookBean>)request.getAttribute("list"); // 判断集合是否有效 if (list == null || list.size() < 1) { out.print("<tr><td bgcolor='#FFFFFF' colspan='5'>没有任何图书信息!</td></tr>"); } else { // 遍历图书集合中的数据 for (BookBean book : list) { %> <tr align="center"> <td bgcolor="#FFFFFF" ><%=book.getId()%></td> <td bgcolor="#FFFFFF"><%=book.getName()%></td> <td bgcolor="#FFFFFF"><%=book.getPrice()%></td> <td bgcolor="#FFFFFF"><%=book.getBookCount()%></td> <td bgcolor="#FFFFFF"><%=book.getAuthor()%></td> </tr> <%} }%> <%=request.getAttribute("bar") %>
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/1381.html