JSP分页代码(最原始的分页思想)

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.sql.*" %>
<%
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>仿BAIDU & GOOGLE 分页页码显示格式</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">
<style>
<!--
body { .p { PADDING-LEFT:18px;
FONT-SIZE: 14px;
WORD-SPACING: 4px
}
}
-->
</style>
</head>

<body>
<div class="p">
<%
int pagesize = 10;//每页显示记录数
int liststep = 20;//最多显示分页页数
int pages = 1;//默认显示第一页
if (request.getParameter("pages") != null) {
pages = Integer.parseInt(request.getParameter("pages"));//分页页码变量
}
//sql="select count(*) from table";//取出记录总数,把*换成记录名性能更加
int count = 305;//假设取出记录总数
int pagescount = (int) Math.ceil((double) count / pagesize);//求总页数,ceil(num)取整不小于num
if (pagescount < pages) {
pages = pagescount;//如果分页变量大总页数,则将分页变量设计为总页数
}
if (pages < 1) {
pages = 1;//如果分页变量小于1,则将分页变量设为1
}
int listbegin = (pages - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
if (listbegin < 1) {
listbegin = 1;
}
int listend = pages + liststep/2;//分页信息显示到第几页
if (listend > pagescount) {
listend = pagescount + 1;
}

//显示数据部分
int recordbegin = (pages - 1) * pagesize;//起始记录
int recordend = 0;
recordend = recordbegin + pagesize;
//最后一页记录显示处理
if (pages == pagescount) {
recordend = (int) (recordbegin + pagesize * (count % pagesize) * 0.1);
}

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strsql = "select top "+pagesize+ " * from (select top "+recordbegin+" * from tables order by aml_id) aa order by aml_id desc";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:", "", "");
pstmt = conn.prepareStatement(strsql);
rs = pstmt.executeQuery();
while(rs.next()){
response.getWriter().println("AML_ID: "+rs.getString(1)+" ");//显示取出的数据
response.getWriter().println("AML_ID: "+rs.getString(4)+"<br>");//显示取出的数据
}


// for (int i = recordbegin; i < recordend; i++) {
// response.getWriter().println("record " + i + "<br>");
// }

//显示数据部分

//<显示分页信息
//<显示上一页
if (pages > 1) {
response.getWriter().println(
"<a href=?pages=" + (pages - 1) + ">上一页</a>");
}//>显示上一页
//<显示分页码
for (int i = listbegin; i < listend; i++) {
if (i != pages) {//如果i不等于当前页
response.getWriter().println(
"<a href=?pages=" + i + ">[" + i + "]</a>");
} else {
response.getWriter().println("[" + i + "]");
}
}//显示分页码>
//<显示下一页
if (pages != pagescount) {
response.getWriter().println(
"<a href=?pages=" + (pages + 1) + ">下一页</a>");
}//>显示下一页
//>显示分页信息
rs.close();
pstmt.close();
conn.close();
%>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值