我都有点不好意思发表了 我这是最没技术含量的 刚写好,经过测试 <%@ page language="java" import="java.util.*,java.sql.Connection,util.JdbcUtil,java.sql.SQLException " pageEncoding="utf-8"%><%@page import="java.sql.Statement"%><%@page import="java.sql.ResultSet"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>
<title>My JSP 'Test.jsp' starting page</title>
</head>
<body> <% /*************************************/
int nowpage=1; //nowpage为当前的页面号 if(request.getParameter("dipage")!=null){ nowpage=Integer.valueOf(request.getParameter("dipage")).intValue(); }
/**********************************/ %>
<center> This is my JSP-SQL Server Test Page. <br> <table border="1px red solid"> <tr> <td colspan="5"><center>学生信息统计</center></td> </tr> <tr> <td>学号</td> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>生日</td> </tr>
<% Connection conn = null; Statement st = null; ResultSet rs = null; try { conn = JdbcUtil.getConnection(); st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); rs =st.executeQuery("select * from STUDENT");
/*************** 下面开始分页的内容*****************/ rs.last(); int countRecord = rs.getRow();//countRecord得到结果集ResultSet里面的记录数 int everyPageRecord = 2; //everyPageRecord:每一页显示的记录数 int countPage = 0; //countPage 总共的页数 /*下面来计算countPage*/ if(countRecord%everyPageRecord==0){ countPage = countRecord/everyPageRecord; }else{ countPage = countRecord/everyPageRecord+1; } out.print("countRecord="+countRecord+"countPage="+countPage);
rs.beforeFirst();//将记录指针移动到第一页之前
int point = 0;//point为记录指针移动的次数 if(nowpage==1);//如果当前是第1页,什么也不做 else{ //否则resule指针移动到自己想要的位置 while(rs.next()){ point++; if(point==((nowpage-1)*everyPageRecord)){ //将result的指针移动到所要显示的页面的第一行数据的前一项数据 out.print("当前的point="+point); break; } } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>书城主页</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head> <body>
<div id="header" class="wrap">
<div id="banner"></div>
<div id="navbar">
<div class="userMenu">
<ul>
<li class="current">
<c:choose>
<c:when test="${empty user.userName}">
<font><a href="login.jsp" style="float: left;">登录 ||</a>
<a href="register.jsp" style="float: left;">注册</a></font>
</c:when>
<c:otherwise>
<font color="BLACK">欢迎您,<strong>${user.userName}</strong></font>
</c:otherwise>
</c:choose>
</li>
<li>
首页
</li>
<li>
<a href="orderlist.jsp">我的订单</a>
</li>
<li>
<a href="cart.jsp">购物车</a>
</li>
<li>
<a href="logout.jsp">注销</a>
</li>
</ul>
</div>
</div>
</div>
<div id="content" class="wrap">
<div class="list bookList">
<form method="post" name="shoping" action="CartBookServlet.do">
<table>
<tr class="title">
<th class="checker"></th>
<th>书名</th>
<th class="info">简介</th>
<th class="price">价格</th>
<th class="store">库存</th>
<th class="view">图片预览</th>
</tr>
<c:choose>
<c:when test="${empty books}">
<tr><td><h2>暂无图书</h2></td></tr>
</c:when>
<c:otherwise>
<c:forEach items="${books}" var="book">
<tr>
<td>
<input type="checkbox" name="bookId" value="${book.bid}" />
</td>
<td class="title">
${book.bookname }
<input type="text" name="uid" value="${book.bookname }" style="display: none;" />
</td>
<td class="info">
${book.info }
<input type="text" name="info" value="${book.info }" style="display: none;" />
</td>
<td>
¥${book.price }
<input type="text" name="price" value="${book.price }" style="display: none;" />
</td>
<td>
${book.stock }
</td>
<td class="thumb">
<img src="images/book/${book.image }" />
<input type="text" name="image" value="${book.image }" style="display: none;" />
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<!-- 分页 -->
<div class="page-spliter">
<a href="ListBookServlet.do?pageIndex=1">首页</a>
<c:forEach begin="1" end="${totalPages}" varStatus="num">
<c:choose>
<c:when test="${currentPage == num.count}">
<span class="current">${num.count}</span>
</c:when>
<c:otherwise>
<a href="ListBookServlet.do?pageIndex=${num.count}">${num.count}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<a href="ListBookServlet.do?pageIndex=${totalPages}">尾页</a>
</div>
<div class="button">
<input class="input-btn" type="submit" name="submit" value="" />
</div>
</form>
</div>
</div>
<div id="footer" class="wrap">
网上书城 © 版权所有
</div>
</body>
</html>
很详细了~我个人觉得,以后你还会写很多项目,很多地方要用到分页,所以我建议你自己写一个分页标签,使用的就是tag。这样呢,以后在项目中,直接把你自己写的这个配置过去,然后就可以完成分页信息了。
不用每次都写链接,做计算什么的~
tag就是标签,我给你贴一个我曾经自己写的吧,仅供参考哦~// 这里是tag标签的代码
/*
* Copyright: @ yl_mu 2010
*
* Created on 2010-9-17 下午01:21:54
*/
package cn.yanmushi.util;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;/**
* 类功能描述:分页显示数据
*
* @author <a href="mailto:[email protected]">艳沐石 </a>
* @version $Id: java_code_templates.xml,v 1.1.4.1 2010/07/23 07:52:13 juchen
* Exp $ Create: 2010-9-17 下午01:21:54
*/
public class Paging extends TagSupport { private static final long serialVersionUID = -7828324903896540155L; private String toUrl; // 跳转路径
private int currentPage; // 当前页面
private int totalPage; // 总页面
private String param; // 查询参数 private String url; // 链接路径 /**
* @return the toUrl
*/
public String getToUrl() {
return toUrl;
} /**
* @param toUrl
* the toUrl to set
*/
public void setToUrl(String toUrl) {
this.toUrl = toUrl;
} /**
* @return the currentPage
*/
public int getCurrentPage() {
return currentPage;
} /**
* @param currentPage
* the currentPage to set
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} /**
* @return the totalPage
*/
public int getTotalPage() {
return totalPage;
} /**
* @param totalPage
* the totalPage to set
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
} /**
* @return the param
*/
public String getParam() {
return param;
} /**
* @param param
* the param to set
*/
public void setParam(String param) {
this.param = param;
} /*
* (non-Javadoc)
*
* @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
*/
@Override
public int doEndTag() throws JspException {
int index = getCurrentPage() - 2; // 数字快捷键的游标
JspWriter out = super.pageContext.getOut();
confUrl(); // 配置链接路径 try {
// 当前页大于“1”的时候
// 添加跳转到“首页”的符号
// 添加跳转到“上一页”的符号
if (getCurrentPage() > 1) {
out.print(getDefaultTagIcon(1, "首页 "));
out
.print(getDefaultTagIcon(getCurrentPage() - 1,
" 上一页 "));
} // 显示以当前页为中心,左右的快捷数字键
for (int i = 0; i < 5 && index <= getTotalPage(); i++) {
// 如果游标小于1的时候,跳过添加标签
if (index < 1) {
index++;
continue;
}
// 将数字键值与当前页进行比较
// 如果相等,则将数字键突出显示
// 否则,进行默认显示
if (index == getCurrentPage()) {
out.print(getCurrentPageTagIcon(index, index + " "));
} else {
out.print(getDefaultTagIcon(index, index + " "));
} index++; // 游标加1
} // 当前页小于“总页数”的时候
// 添加跳转到“末页”的符号
// 添加跳转到“下一页”的符号
if (getCurrentPage() < getTotalPage()) {
out.print(getDefaultTagIcon(getCurrentPage() + 1, "下一页 "));
out.print(getDefaultTagIcon(getTotalPage(), " 末页"));
}
} catch (Exception e) {
e.printStackTrace();
} return EVAL_PAGE; // 继续执行页面的其他内容
} /*
* 配置链接路径
*/
private void confUrl() {
url = getToUrl() + "?"; // 如果有查询参数,将查询参数添加的链接路径中
if (getParam() != null) {
url += getParam();
}
} /*
* 获得默认的标签图标
*/
private String getDefaultTagIcon(int pagenum, String tagIcon) {
return confSingleTag(pagenum, tagIcon, 15, "#000000");
}
/*
* 获得当前页的标签图标
*/
private String getCurrentPageTagIcon(int pagenum, String tagIcon) {
return confSingleTag(pagenum, tagIcon, 20, "red");
}
/*
* 配置标签字符
*/
private String confSingleTag(int pagenum, String tagIcon, int fontSize,
String color) {
return "<a href=\"" + url + "&dp.currentPage=" + pagenum + "&dp.totalPage="
+ getTotalPage() + "\" style=\"font-size: " + fontSize
+ "px; color: " + color + ";\">" + tagIcon + "</a>";
}
}<!-- 这里配置的是标签的tld文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>psc</short-name> <!-- 配置分页标签 -->
<tag>
<name>paging</name>
<tag-class>cn.edu.tjuci.util.page.Paging</tag-class>
<body-content>empty</body-content>
<attribute>
<name>toUrl</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>currentPage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>totalPage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>param</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<!-- 将tld添加到web.xml文件中 -->
<jsp-config>
<taglib>
<taglib-uri>/pc</taglib-uri>
<taglib-location>/WEB-INF/tlds/pageControl.tld</taglib-location>
</taglib>
</jsp-config>
我这是最没技术含量的
刚写好,经过测试
<%@ page language="java" import="java.util.*,java.sql.Connection,util.JdbcUtil,java.sql.SQLException " pageEncoding="utf-8"%><%@page import="java.sql.Statement"%><%@page import="java.sql.ResultSet"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'Test.jsp' starting page</title>
</head>
<body>
<%
/*************************************/
int nowpage=1; //nowpage为当前的页面号
if(request.getParameter("dipage")!=null){
nowpage=Integer.valueOf(request.getParameter("dipage")).intValue();
}
/**********************************/
%>
<center>
This is my JSP-SQL Server Test Page. <br>
<table border="1px red solid">
<tr>
<td colspan="5"><center>学生信息统计</center></td>
</tr>
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>生日</td>
</tr>
<%
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConnection();
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs =st.executeQuery("select * from STUDENT");
/*************** 下面开始分页的内容*****************/
rs.last();
int countRecord = rs.getRow();//countRecord得到结果集ResultSet里面的记录数
int everyPageRecord = 2; //everyPageRecord:每一页显示的记录数
int countPage = 0; //countPage 总共的页数
/*下面来计算countPage*/
if(countRecord%everyPageRecord==0){
countPage = countRecord/everyPageRecord;
}else{
countPage = countRecord/everyPageRecord+1;
}
out.print("countRecord="+countRecord+"countPage="+countPage);
rs.beforeFirst();//将记录指针移动到第一页之前
int point = 0;//point为记录指针移动的次数
if(nowpage==1);//如果当前是第1页,什么也不做
else{ //否则resule指针移动到自己想要的位置
while(rs.next()){
point++;
if(point==((nowpage-1)*everyPageRecord)){ //将result的指针移动到所要显示的页面的第一行数据的前一项数据
out.print("当前的point="+point);
break;
}
}
}
point = 0 ; //重新计数
while(rs.next()){
out.print("<tr>");
out.print("<td>");out.print(rs.getInt(1));out.print("</td>");
out.print("<td>");out.print(rs.getString(2));out.print("</td>");
out.print("<td>");out.print(rs.getString(3));out.print("</td>");
out.print("<td>");out.print(rs.getInt("STUAGE"));out.print("</td>");
out.print("<td>");out.print(rs.getDate("STUBIRTHDAY"));out.print("</td>");
out.print("</tr>");
point++;
if(point==everyPageRecord){
break;
}
}
out.print("<tr>");
out.print("<td colspan='5'><center>");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+nowpage+"页,");
if(nowpage==1);//当前是首页,什么也不做
else{ //否则
out.print("<a href=Test.jsp?dipage=1>首页</a>");
out.print("<a href=Test.jsp?dipage="+(nowpage-1)+">上一页</a>");
}
if(nowpage==countPage);//当前是末页,什么也不做
else{ //否则
out.print("<a href=Test.jsp?dipage="+(nowpage+1)+">下一页</a>");
out.print("<a href=Test.jsp?dipage="+countPage+">末页</a>");
}
out.print("</center></td>");
out.print("</tr>");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtil.free(rs, st, conn);
}
%>
</table>
<center>
</body>
</html>
jquery也有一些分页的例子!