如何使用Servlet实现Java分页

Java分页

首先说一下什么是分页:分页就是将所有数据分开展示给用户的技术,通俗点说就像大家的书一样,我们可以通过页数来找到自己想要的内容,Java分页也是如此。

Java分页的意义

分页确实有效,但它一定会加大系统的复杂度,但可否不分页呢?如果学习的话当然可以.,因为数据库的数据比较少;但是对于企业信息系统来说数据量不会限制在一个小范围内,利用分页可以高效的,找到数据,并且减轻对服务器的压力。

如何实现分页

首先我们需要一个工具类Page.Java来做辅助,通过封装来限制每页的数据量,总数据量,和每页的页码,最后保存到list集合中,进行读取。

public class Page {
	//总页数
	private int totalPageCount = 0;
	//每页显示记录数
	private int pageSize = 3;
	//当前总数
	private int totalCount;
	//当前页码
	private int currPageNo = 1;
	//每页的集合
	private List<Student> studentList;
	public int getTotalPageCount() {
		return totalPageCount;
	}
	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		if(pageSize>0)
			this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		if(totalCount>0)
			this.totalCount = totalCount;
			//计算总页数
			totalPageCount=this.totalCount%pageSize==0?(this.totalCount/pageSize):(this.totalCount/pageSize+1);
	}
	public int getCurrPageNo() {
		if(totalPageCount==0)
			return 0;
		return currPageNo;
	}
	public void setCurrPageNo(int currPageNo) {
		if(currPageNo>0)
			this.currPageNo = currPageNo;
	}
	public List<Student> getStudentList() {
		return studentList;
	}
	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

}

我们在这里需要自己搭三层
然后我们来到Servlet页面

package cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
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.alibaba.fastjson.JSON;

import cn.dao.StudentDao;
import cn.dao.impl.StudentDaoImpl;
import cn.entity.Page;
import cn.entity.Student;

public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	response.setContentType("text/html;charset=utf-8");
	//从页面接受页码
	String  strNum=request.getParameter("no");
	int no=1;
	if(strNum==null || strNum==""){			
	}else{
		no=Integer.parseInt(strNum);
	}

	StudentDao dao = new StudentDaoImpl();
	//总数量   从数据库里面查出来的
	int totalCount = dao.getTotalCount();
	
	Page p = new Page();
	//当前页面
	p.setCurrPageNo(no);
	//当前总数
	p.setTotalCount(totalCount);
	//查询当前页面的   limit ?,?
	List<Student> list = dao.getPageList(p.getCurrPageNo(), p.getPageSize());
	//赋给page里面的List
	p.setStudentList(list);
	request.setAttribute("list", p.getStudentList());
	request.setAttribute("count", p.getTotalPageCount());
	//当前页码
	request.setAttribute("no",p.getCurrPageNo());
	request.getRequestDispatcher("show.jsp").forward(request, response);
	
	
	
	/*out.print("新闻总数量:"+p.getTotalCount()+"<br/>");
	out.print("每条数量是"+p.getPageSize()+"<br/>");
	out.print("总页数"+p.getTotalPageCount()+"<br/>");
	out.print("当前是第"+p.getCurrPageNo()+"页<br/>");
	List<Student> list = dao.getPageList(p.getCurrPageNo(), p.getPageSize());
	p.setStudentList(list);
	String json = JSON.toJSONString(p.getStudentList());
	out.print(json);*/
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	doGet(request, response);
}

 }

最后来到Show.jsp页面

<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"😕/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<title>My JSP 'show.jsp' starting page</title>
<table>
	<tr>
		<td>编号</td>
		<td>姓名</td>
		<td>密码</td>
	</tr>
	<c:forEach items="${list }"  var="item">
		<tr>
				<td>${item.id }</td>
				<td>${item.name }</td>
				<td>${item.pwd }</td>
		</tr>
	
	</c:forEach>
	<tr>
	<td><a href="a?no=${no-1 }">上一页</a></td>
	<td><a href="a?no=${no+1 }">下一页</a></td>
	<td><a href="a?no=1 ">首页</a></td>
	<td><a href="a?no=${count} ">尾页</a></td>
	</tr>
	
	
</table>

关于数据库,我们使用的是Mysql。表中有三个属性:编号,姓名,密码
然后加几条测试数据,给大家发一下我的效果图

Tomcat我用的是7.x,其他版本没有试过,如果过程中有什么不懂的可以加我微信号:lxl1052681394
或者扫下面二维码
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章