1、java servlet分页操作
2、main.jsp页面
<tr>
<td colspan="9" align="center">
<a href="/page?start=0">[首 页]</a> |
<a href="/page?start=${pre}">[上一页]</a> |
<a href="/page?start=${next}">[下一页]</a> |
<a href="/page?start=${last}">[末 页]</a>
</td>
</tr>
3、PageServlet
package com.servlet.bbs;
import com.dao.BbsDao;
import com.entity.BbsEntity;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* 分页
* Created by lvjun on 2018-03-22.
*/
@WebServlet(name = "PageServlet")
public class PageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
int start = 0;
int count = 10;
try {
start = Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException e) {
}
int next = start + count;
int pre = start - count;
int total = new BbsDao().GetTotal();
int last;
if (0 == total % count)
last = total - count;
else
last = total - total % count;
pre = pre < 0 ? 0 : pre;
next = next > last ? last : next;
request.setAttribute("total", total);
request.setAttribute("last", last);
request.setAttribute("next", next);
request.setAttribute("pre", pre);
List<BbsEntity> list = new BbsDao().GetPageList(start, count);
request.setAttribute("list", list);
request.getRequestDispatcher("/main.jsp").forward(request, response);
}
}