开始显示的是1,2,3,4,5 第二页就是当我单击2,3的时候显示的还是1,2.3,4,5 单击4的时候显示的则是2,3,4,5,6 5的时候显示的是3,4,5,6,7 6的时候是4,5,6,7,8
package com.kt.servlet_qt;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kt.bean.Easybuy_Shangpin;
import com.kt.dao.impl.Easybuyimpl;
public class ecommodityservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Easybuyimpl e = new Easybuyimpl();
// 显示多少条数据
int pagesize = 8;
// 获取商品总记录数
int record = e.total();
// 计算商品有多少页数
int totalPage = record % pagesize == 0 ? (record / pagesize) : (record
/ pagesize + 1);
// 获取多少页
String curpage = request.getParameter("curpage");
// 当前页
int count = -1;
if (curpage != null) {
count = Integer.parseInt(curpage);
// 判断当前页是否小于0
if (Integer.parseInt(curpage) < 1) {
count = 1;
}
// 判断当前页数是否超出数据现有页数
if (Integer.parseInt(curpage) >= totalPage) {
count = totalPage;
}
} else {
count = 1;
}
// 分页
List<Easybuy_Shangpin> findall_ShangpinFenye = e.findall_ShangpinFenye(
(count - 1) * pagesize, pagesize);
// 总页数
request.getSession().setAttribute("totalPage", totalPage);
// 当前页数
request.getSession().setAttribute("count", count);
// 页数的数据
request.getSession().setAttribute("findall_ShangpinFenye",
findall_ShangpinFenye);
request.getRequestDispatcher("product-list.jsp").forward(request,
response);
out.flush();
out.close();
}
}<div class="pager">
<ul class="clearfix">
<li><a href="ecommodityservlet?curpage=${count-1}">上一页</a></li>
<c:choose>
<c:when test="${count<=3 }">
<c:forEach begin="0" end="4" var="v">
<c:if test="${count==v+1}">
<li><a href="ecommodityservlet?curpage=${count-1}"
style="color: red">${v+1 }</a>
</li>
</c:if>
<c:if test="${count!=v+1}">
<li><a href="ecommodityservlet?curpage=${v+1}">${v+1
}</a>
</li>
</c:if>
</c:forEach>
</c:when>
<c:when test="${count < totalPage-3 }">
<c:forEach begin="${count-2 }" end="${count+2 }" var="v">
<c:if test="${count==v+1 }">
<li><a href="ecommodityservlet?curpage=${v+1}"
style="color: red">${v+1 }</a>
</li>
</c:if>
<c:if test="${count!=v+1}">
<li><a href="ecommodityservlet?curpage=${v+1}">${v+1
}</a>
</li>
</c:if>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach begin="${totalPage-5 }" end="${totalPage-1 }" var="v">
<c:if test="${count==v+1 }">
<li><a href="ecommodityservlet?curpage=${v+1}"
style="color: red">${v+1 }</a>
</li>
</c:if>
<c:if test="${count!=v+1 }">
<li><a href="ecommodityservlet?curpage=${v+1}">${v+1
}</a>
</li>
</c:if>
</c:forEach>
</c:otherwise>
</c:choose>
<li><a href="ecommodityservlet?curpage=${ count+1}">下一页</a></li>
</ul>
</div>
</div>
</div>原文地址:http://flyblog.blog.51cto.com/10081495/1768389