首先在浏览器中输入http://localhost:8080/stu/StudentListPageServlet?currentPage=1---->系统调用【StudentListPageServlet】---->调用【StudentService.findStudentByPage()】---->调用多个【StudentDao.XXX】方法---->将分页信息存入PageBean对象,返回到页面【list_page.jsp】页面显示。以下按上述思路将代码展示,数据库连接在最后。
StudentListPageServlet.java
package com.itheima.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.valves.StuckThreadDetectionValve;
import com.itheima.domain.PageBean;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
/**
* 这是用于分页显示学生列表的servlet
* @author xiaomi
*
*/
public class StudentListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 获取需要显示的页码数
int currentPage =Integer.parseInt( request.getParameter("currentPage"));
//2. 根据指定的页数,去获取该页的数据回来
//List<Student> --- list.jsp
StudentService service = new StudentServiceImpl();
PageBean pageBean= service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3. 跳转界面。
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
StudentService.java
package com.itheima.service;
import java.sql.SQLException;
import java.util.List;
import com.itheima.domain.PageBean;
import com.itheima.domain.Student;
/**
* 这是学生的业务处理规范
* @author xiaomi
*
*/
public interface StudentService {
/**
* 查询当页的数据
* @param currentPage
* @return
* @throws SQLException
*/
PageBean findStudentByPage(int currentPage) throws SQLException ;
}
StudentServiceImpl.java
package com.itheima.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.itheima.dao.StudentDao;
import com.itheima.dao.impl.StudentDaoImpl;
import com.itheima.domain.PageBean;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;
/**
* 这是学生业务实现
* @author xiaomi
*
*/
public class StudentServiceImpl implements StudentService{
@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {
//封装分页的该页数据
PageBean<Student> pageBean = new PageBean<Student>();
int pageSize = StudentDao.PAGE_SIZE ;
pageBean.setCurrentPage(currentPage); //设置当前页
pageBean.setPageSize(pageSize); //设置每页显示多少记录
StudentDao dao = new StudentDaoImpl() ;
List<Student> list =dao .findStudentByPage(currentPage);
pageBean.setList(list); //设置这一页的学生数据
//总的记录数, 总的页数。
int count = dao.findCount();
pageBean.setTotalSize(count); //设置总的记录数
//200 , 10 ==20 201 , 10 = 21 201 % 10 == 0 ?201 / 10 :201 % 10 + 1
pageBean.setTotalPage(count % pageSize=