===========访问地址
项目路径/mulPageSystem
===========java文件;包名org.rain.bean 文件名 PageSystemBean
package org.rain.bean;
public class PageSystemBean {
private String school;
private String home;
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
}
===========java文件;包名org.rain.servlet 文件名 MulPage
package org.rain.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
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 javax.servlet.http.HttpSession;
import org.rain.bean.PageSystemBean;
@WebServlet("/mulPageSystem")
public class MulPage extends HttpServlet {
private static final long serialVersionUID = 1L;
List<PageSystemBean> smallRainPageSystemBeans;
int smallRainTotalRecord=45;
int smallRainSize = 10;
int smallRainCurrent = 1;
@Override
public void init(ServletConfig config) throws ServletException{
smallRainPageSystemBeans=new ArrayList<PageSystemBean>(smallRainTotalRecord);
for(int i=0;i<smallRainTotalRecord;i++){
PageSystemBean bean=new PageSystemBean();
bean.setSchool("大学"+i);
bean.setHome("地址"+i);
smallRainPageSystemBeans.add(i, bean);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String smallRainpage = request.getParameter("smallRainpage");
request.getSession().setAttribute("URIRight", "right uri");
HttpSession sms=request.getSession();
if (smallRainpage != null) {
smallRainCurrent = Integer.parseInt(smallRainpage);
request.setAttribute("smallRainpage", smallRainCurrent);
}else {
request.setAttribute("smallRainpage", 1);
smallRainCurrent=1;
}
int smallRainDo = (smallRainCurrent - 1) * smallRainSize;
int totalPageBeans = smallRainPageSystemBeans.size();
int totalPages =totalPageBeans / smallRainSize;
totalPages++;
List<PageSystemBean> smallRainCurrentPageBeans;
if(totalPages==smallRainCurrent){
smallRainCurrentPageBeans = smallRainPageSystemBeans.subList(smallRainDo, totalPageBeans);
}else{
smallRainCurrentPageBeans = smallRainPageSystemBeans.subList(smallRainDo, smallRainDo+smallRainSize);
}
request.setAttribute("smallRainCurrentPageBeans", smallRainCurrentPageBeans);
request.setAttribute("smallRainCurrent", smallRainCurrent);
request.setAttribute("totalPages", totalPages);
request.setAttribute("smallRainURI", request.getServletPath().substring(1));
request.getRequestDispatcher("/MulPageSystemServlet.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.rain.bean.PageSystemBean,java.util.List,java.util.ArrayList" %>
<html>
<head>
<meta charset="UTF-8">
<title>晓雨 JSP+SERVLET 实现分页</title>
</head>
<body>
<h1> 晓雨 JSP+SERVLET 实现分页</h1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="EFF6EFE">
<td width="50" height="45" ><b>学校名</b></td>
<td width="50" height="45" ><b>家乡名称</b></td>
</tr>
<%
Object smsession=request.getSession().getAttribute("URIRight");
Object smsessionRequest=request.getAttribute("smallRainpage");
if(null!=smsession&&smsessionRequest!=null){
int backColor=0;
List<PageSystemBean> beans=(List<PageSystemBean>)request.getAttribute("smallRainCurrentPageBeans");
for (PageSystemBean smallRainPageBean : beans) {
backColor++;
if(backColor%2==0){
%>
<tr bgcolor="EFFEEFE">
<%}else{%>
<tr >
<%}%>
<td width="50" height="35" ><%=smallRainPageBean.getSchool() %></td>
<td width="50" height="35" ><%= smallRainPageBean.getHome() %></td>
</tr>
<% } %>
<% } %>
</table>
<% Object smsessionTable=request.getSession().getAttribute("URIRight");
Object smsessionrequestTable=request.getAttribute("smallRainpage");
if(null!=smsessionTable&&smsessionrequestTable!=null){
%>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<b>
<%
String smallRainURI=(String)request.getAttribute("smallRainURI");
int smallRainCurrent=(int)request.getAttribute("smallRainCurrent");
int smallRaintotalPages=(int)request.getAttribute("totalPages");
if (smallRainCurrent > 1) { %>
<a href="<%=smallRainURI%>?smallRainpage=<%= smallRainCurrent - 1 %>">上一页</a>
<% } %>
<%
for (int i = 1; i <= smallRaintotalPages; i++) { %>
<% if (i == smallRainCurrent) { %>
<span><%= i %></span>
<% } else { %>
<a href="<%=smallRainURI%>?smallRainpage=<%= i %>"><%= i %></a>
<% } %>
<% } %>
<% if (smallRainCurrent < smallRaintotalPages) { %>
<a href="<%=smallRainURI%>?smallRainpage=<%= smallRainCurrent + 1 %>">下一页</a>
<% } %>
</b>
</table>
<% }
%>
</body>
</html>