类似百度谷歌分页算法实现

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.modorn.Bean.UserDao"%>
<%@page import="com.modorn.Bean.UserBean"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'manger.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
table{ border-collapse:collapse; }
td{ border:#ccc solid 1px; padding:5px; }
</style>

	</head>

	<body>
		<%!int pageNow = 1;%>

		<%
			String userName = (String) request.getSession().getAttribute("userName");
			if (userName == null) {
				response.sendRedirect("login.jsp?erro=1");
			}
			if (request.getParameter("pageNow") != null) {
				pageNow = Integer.parseInt(request.getParameter("pageNow"));
			}
		%>
		<center>

			欢迎
			<font color=red><%=userName%></font>登录
			<br />
			<br />
			<br />
			<%
				UserDao userDao = new UserDao();
				int pageNum = userDao.getPageNum();
				ArrayList<UserBean> list = userDao.fenyeInfo(pageNow);
				int countPage = userDao.getCounPage();
			%>

			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<th>
						用户ID
					</th>
					<th>
						用户名
					</th>
					<th>
						密码
					</th>
					<th>
						用户类型
					</th>
				</tr>
				<%
					for (int i = 0; i < list.size(); i++) {
						out.println("<tr><td>" + list.get(i).getUserId() + "</td>");
						out.println("<td>" + list.get(i).getUserName() + "</td>");
						out.println("<td>" + list.get(i).getPassW() + "</td>");
						out.println("<td>" + list.get(i).getGrade() + "</td></tr>");
					}
				%>
			</table>
			
			<!--分页算法开始-->

			<%
				if (pageNow != 1) {
					out.println("<a href=manger.jsp?pageNow=" + (pageNow - 1)
							+ ">【上一页】</a>");
				}
				int start;

				if(pageNow < pageNum){
					start = 1;
				}else{
					start = pageNow - 10 + 1;
				}
				
				for (int i = start; i <= countPage && i <= pageNow + pageNum; i++) {
					if(i == pageNow){
						out.println(i);
						continue;
					}
					out.println("<a href=manger.jsp?pageNow=" + i + ">[" + i
							+ "]</a>");
				}
				if (pageNow != countPage) {
					out.println("<a href=manger.jsp?pageNow=" + (pageNow + 1)
							+ ">【下一页】</a>");
				}
			%>
			<!--分页算法结束-->
		</center>

	</body>
</html>
 

相关推荐