httpservlet里单纯分页

时间:2022-07-15 22:38:51
    @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="data: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>