servlet:
package cn.csdn.web.servlet;
import java.io.IOException;
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.csdn.web.service.StudentServiceImpl;
import cn.scdn.domain.Student;
public class ListStusServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1.设置编码
req.setCharacterEncoding("utf8");
int startindex = 1;
int endindex = 3;
// 2.获取当前页
int nowpage = 1;
String npage = req.getParameter("nowpage");
if (npage != null) {
nowpage = Integer.valueOf(npage);
}
// 3、创建sevice服务操作对象
StudentServiceImpl ssi = new StudentServiceImpl();
int lastpage = ssi.getCountPage();
// ================================我做的
// 判断是不是后一页
if(endindex<lastpage){
if(endindex>6){
// 如果大于6的话,startindex要变大
}
// endindex要变大
}else{
if(nowpage<lastpage-6){
// endindex不变,startindex要变小
}
}
// ==========================
if (endindex > lastpage) {
endindex = lastpage;
} else {
endindex = nowpage + 1;
if (endindex != 3) {
startindex = nowpage - 1;
}
if (startindex < 1) {
startindex = 1;
endindex = 3;
}
}
List<Student> allentities = ssi.getNowPageInfo1(nowpage);
// 4、存入到request中
req.setAttribute("nowpage", nowpage);
req.setAttribute("allentities", allentities);
req.setAttribute("lastpage", lastpage);
// 5、存入索引值
req.setAttribute("startindex", startindex);
req.setAttribute("endindex", endindex);
req.setAttribute("startindex", startindex);
req.getRequestDispatcher("liststs.jsp").forward(req, resp);
}
}
index.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<a href="listStudents.do">分页查询所有的学生信息方式实现1</a>
<a href="listSts.do">分页查询所有的学生信息方式实现2</a>
</body>
</html>
跳转到liststs.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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'sc.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<h1>显示所有的学员信息</h1>
<table border="1px" cellpadding="0" cellspacing="0">
<caption>学员信息</caption>
<tr>
<th>全选</th>
<th>序列</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${allentities}" var="entity">
<tr>
<td><input type="checkbox" name="chk"/></td>
<td>${entity.id}</td>
<td>${entity.name}</td>
<td>${entity.age}</td>
<td>${entity.email}</td>
<td>编辑|删除</td>
</tr>
</c:forEach>
</table>
<div>
<span>
<c:if test="${nowpage>1}">
<a href="${pageContext.request.contextPath}/listSts.do?nowpage=${nowpage-1>0?nowpage-1:1}">上一页</a>
</c:if>
</span>
<c:forEach begin="${startindex}" end="${endindex}" var="indexnum">
<a href="${pageContext.request.contextPath}/listSts.do?nowpage=${indexnum}">
[<span>${indexnum}</span>]
</a>
</c:forEach>
<span><a href="${pageContext.request.contextPath}/listSts.do?nowpage=${nowpage+1>lastpage?lastpage:nowpage+1}">下一页</a></span>
</div>
</div>
</body>
</html>