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

jsp学习笔记(22)
PageServlet代码如下:
	package servlet.user;
	import java.io.IOException;
	import java.util.ArrayList;
	import javax.servlet.RequestDispatcher;
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import dao.DBOperation;
	import util.PageUtil;
	public class PageServlet extends HttpServlet {
		public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			DBOperation dbo=new DBOperation();
			ArrayList<?> arrayList = null;
			int recordCount = 0;
			int currentPage = 1;
			String sqlCount="select count(*) from books";
			recordCount = dbo.getCount(sqlCount);
			String pageStr = request.getParameter("page");
			if(pageStr!=null)
				currentPage = Integer.parseInt(pageStr);
			PageUtil pageUtil = new PageUtil(5, recordCount);
			pageUtil.setCurrentPage(currentPage);
			String pagenatedSql  = "SELECT TOP "+pageUtil.getPageSize()+" * FROM 
			books WHERE id NOT IN"+"( SELECT TOP "+pageUtil.getPageSize()*
	 		(pageUtil.getCurrentPage()-1)+"id FROM books"+" ORDER BY id) 
			ORDER BY id";
			arrayList = dbo.queryReturnList(pagenatedSql);
			request.setAttribute("search", arrayList);
			request.setAttribute("currentPage", pageUtil.getCurrentPage());
			request.setAttribute("pageCount", pageUtil.getPageCount());
			String str = "/user/bookList.jsp";
			RequestDispatcher rd = request.getRequestDispatcher(str);
			rd.forward(request, response);
		}
		public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
		}
	}