@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //對List數據分頁 req.setAttribute("list", list); int pageNos; if (req.getParameter("pageNos") == null || Integer.parseInt(req.getParameter("pageNos")) < 1) { pageNos = 1; } else { pageNos = Integer.parseInt(req.getParameter("pageNos")); } req.setAttribute("pageNos", pageNos); int size=list.size(); int countPage = size%4==0? size/4:size/4+1; // 總頁數 req.setAttribute("countPage", countPage); req.getRequestDispatcher("search.jsp").forward(req, resp); }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>結果</title> <link href="css/style.css" rel="stylesheet"> </head> <body> <% String 查找的參數 = request.getParameter("查找的參數"); %> <form action="search" method="get" enctype="application/x-www-form-urlencoded"> <img src="image/logo.jpg" height="40px"/> <input id="css1" name="查找的參數" value=<%=查找的參數%> maxlength="30"> <span ><input type="submit" id="css2" value="搜索一下"></span> <c:forEach var="li" items="${list}" begin="${(pageNos-1)*4 }" end="${pageNos*4-1}"> <br> <p>數據 </p> <br/> </c:forEach> <center> <c:if test="${pageNos>1 }"> <a href="search?查找的參數=<%=查找的參數%>&pageNos=1" >首頁</a> <a href="search?查找的參數=<%=查找的參數%>&pageNos=${pageNos-1 }">上一頁</a> </c:if> <c:if test="${pageNos <countPage }"> <a href="search?查找的參數=<%=查找的參數%>&pageNos=${pageNos+1 }">下一頁</a> <a href="search?查找的參數=<%=查找的參數%>&pageNos=${countPage }">末頁</a> </c:if> </center> <h4 align="center">共${countPage}頁 <input type="text" value="${pageNos}" name="pageNos" size="1">頁 <input type="submit" value="go"> </h4> </form>
轉載於猿2048:➝《httpservlet裏單純分頁》php