1.jsp页面:
- <%
- int currentPage = Integer.parseInt(request.getAttribute("currentPage").toString());
- int totalPage = Integer.parseInt(request.getAttribute("totalPage").toString());
- %>
- <div align="center">
- <a href="Query.servlet?currentPage=1">
- <font color='#0000ff'>首页</font>
- </a>
- <a href="Query.servlet?currentPage=<%=(currentPage-1)%>">
- <font color='#0000ff'>上一页</font>
- </a>
- <a href="Query.servlet?currentPage=<%=(currentPage+1)%>">
- <font color='#0000ff'>下一页</font>
- </a>
- <a href="Query.servlet?currentPage=<%=totalPage%>">
- <font color='#0000ff'>尾页</font>
- </a>
- </div>
2.web.xml配置:
- <servlet>
- <servlet-name>Query</servlet-name>
- <servlet-class>com.servlet.QueryServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Query</servlet-name>
- <url-pattern>/Query.servlet</url-pattern>
- </servlet-mapping>
3.QueryServlet类:
- package com.servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- 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.bean.CourseInfo;
- import com.service.CourseService;
- public class QueryServlet extends HttpServlet
- {
- /**
- * {序列号}
- */
- private static final long serialVersionUID = -6413058086073194500L;
- /**
- *
- * {查询所有课程}
- *
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- * @author:LJ
- */
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
- IOException
- {
- CourseService courseService = new CourseService();
- //查询总记录数
- int totalRows = courseService.queryCount();
- CourseInfo courseInfo = new CourseInfo();
- courseInfo.setTotalRows(totalRows);
- //获取页面当前页
- String currentPage = request.getParameter("currentPage");
- if(currentPage != null)
- {
- courseInfo.setCurrentPage(Integer.parseInt(currentPage));
- }
- int page = courseInfo.getCurrentPage();
- int pageSize = courseInfo.getPageSize();
- List<CourseInfo> courseList = new ArrayList<CourseInfo>();
- courseList = courseService.queryAll(page,pageSize);
- request.setAttribute("currentPage", page);
- request.setAttribute("totalPage", courseInfo.getTotalPage());
- request.setAttribute("courseList", courseList);
- request.getRequestDispatcher("main.jsp").forward(request, response);
- }
- }
4.分页bean:PageInfo.java
- package com.bean;
- public class PageInfo
- {
- private int currentPage = 1;//当前页
- private int totalPage ; //总页数
- private int pageSize = 2; //每页显示记录数
- private int totalRows ; //总记录数
- /**
- *
- * {当前页set方法}
- *
- * @param currentPage
- * @author:LJ
- */
- public void setCurrentPage(int currentPage)
- {
- if(currentPage >= this.totalPage)
- {
- this.currentPage = this.totalPage;
- }
- else if(currentPage <= 1)
- {
- this.currentPage = 1;
- }
- else
- {
- this.currentPage = currentPage;
- }
- }
- /**
- *
- * {当前页get方法}
- *
- * @return
- * @author:LJ
- */
- public int getCurrentPage()
- {
- return currentPage;
- }
- /**
- *
- * {总页数set方法}
- *
- * @param totalPage
- * @author:LJ
- */
- public void setTotalPage()
- {
- this.totalPage = (totalRows%pageSize==0)?(totalRows/pageSize):(totalRows/pageSize+1);
- //this.totalPage = (totalRows + pageSize - 1)/pageSize;
- //this.totalPage = (totalRows - 1)/pageSize + 1;
- }
- /**
- *
- * {总页数get方法}
- *
- * @return
- * @author:LJ
- */
- public int getTotalPage()
- {
- return totalPage;
- }
- /**
- *
- * {每页显示记录数set方法}
- *
- * @param pageSize
- * @author:LJ
- */
- public void setPageSize(int pageSize)
- {
- this.pageSize = pageSize;
- }
- /**
- *
- * {每页显示记录数get方法}
- *
- * @return
- * @author:LJ
- */
- public int getPageSize()
- {
- return pageSize;
- }
- /**
- *
- * {总记录数set方法}
- *
- * @param totalRows
- * @author:LJ
- */
- public void setTotalRows(int totalRows)
- {
- this.totalRows = totalRows;
- this.setTotalPage();
- }
- /**
- *
- * {总记录数get方法}
- *
- * @return
- * @author:LJ
- */
- public int getTotalRows()
- {
- return totalRows;
- }
- }