package cn.itcast.customer.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.customer.domain.Customer;
import cn.itcast.customer.domain.PageBean;
import cn.itcast.customer.service.CustomerService;
public class CustomerFindAllByPageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getRemoteAddr());
// 1.默认访问第一页
int pageNum = 1;
String _pageNum = request.getParameter("pageNum");
if (_pageNum != null) {
pageNum = Integer.parseInt(_pageNum);
}
// 2.每页条数 人为定义
int currentPage = 5;
String _currentPage = request.getParameter("currentPage");
if (_currentPage != null) {
currentPage = Integer.parseInt(_currentPage);
}
// 3.调用service,完成查询当前页数据操作
CustomerService service = new CustomerService();
try {
PageBean pb = service.findByPage(pageNum, currentPage);
// 4.将数据存储到request域,请求转到到页面展示。
request.setAttribute("pb", pb);
request.getRequestDispatcher("/showCustomerByPage.jsp").forward(
request, response);
return;
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
showCustomerByPage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="my" uri="http://www.itcast.cn/tag"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
function changeCurrentPage(value) {
location.href = "/day20_1/findAllByPage?currentPage=" + value;
};
</script>
</head>
<body>
<c:if test="${empty pb.cs}">
无客户信息
</c:if>
<c:if test="${not empty pb.cs}">
<table border="1" align="center" width="85%">
<tr>
<td>客户编号</td>
<td>客户姓名</td>
<td>客户性别</td>
<td>客户生日</td>
<td>客户电话</td>
<td>客户邮箱</td>
<td>客户爱好</td>
<td>客户类型</td>
<td>客户备注</td>
</tr>
<c:forEach items="${pb.cs}" var="c">
<tr>
<td>${c.id }</td>
<td>${c.name}</td>
<td>${c.gender }</td>
<td>${c.birthday }</td>
<td>${c.cellphone }</td>
<td>${c.email }</td>
<td>${c.preference }</td>
<td>${c.type }</td>
<td>${c.description }</td>
</tr>
</c:forEach>
<tr>
<td colspan="9" align="center"><a
href="/day20_1/findAllByPage?pageNum=1¤tPage=${pb.currentPage}">首页</a>
<c:if test="${pb.pageNum==1}">
上一页
</c:if> <c:if test="${pb.pageNum!=1}">
<a
href="/day20_1/findAllByPage?pageNum=${pb.pageNum-1}¤tPage=${pb.currentPage}">上一页</a>
</c:if> <c:if test="${pb.pageNum==pb.totalPage}">
下一页
</c:if> <c:if test="${pb.pageNum!=pb.totalPage}">
<a
href="/day20_1/findAllByPage?pageNum=${pb.pageNum+1 }¤tPage=${pb.currentPage}">下一页</a>
</c:if> <a
href="/day20_1/findAllByPage?pageNum=${pb.totalPage }¤tPage=${pb.currentPage}">尾页</a>
<select name="currentPage"
οnchange="changeCurrentPage(this.value);">
<option>--请选择每页条数--</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
</td>
</tr>
<tr>
<td colspan="9" align="center"><c:forEach begin="1"
end="${pb.totalPage}" var="n" step="1">
<c:if test="${n==pb.pageNum}">
<a
href="/day20_1/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}"><font
color='red'>第${n}页</font> </a>
</c:if>
<c:if test="${n!=pb.pageNum}">
<a
href="/day20_1/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}">第${n}页</a>
</c:if>
</c:forEach>
</td>
</tr>
<tr>
<td colspan="9" align="center"><my:page pb="${pb}" />
</td>
</tr>
</table>
</c:if>
</body>
</html>
来源:CSDN
作者:live801
链接:https://blog.csdn.net/live801/article/details/103951541