導航:首頁 > 編程語言 > jsp頁面翻頁

jsp頁面翻頁

發布時間:2023-08-07 23:47:55

『壹』 jsp翻頁代碼

主要思路就是在點擊換頁的鏈接時給頁面傳是第幾頁的參數,比如你共有total條記錄,每頁pageSize條記錄,那麼第n頁就應該顯示第 (n-1)*pageSize 到第 n*pageSize 條記錄, 共有 total/pageSize+1 頁(因為是int型)

下面是我來的代碼,供參考

int intPageSize; //一頁顯示的記錄數
int intRowCount; //記錄總數
int intPageCount; //總頁數
int intPage; //待顯示頁碼
String strPage;

//設置一頁顯示的記錄數
intPageSize = 25;

//取得待顯示頁碼
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁數據
intPage = 1;
}else{//將字元串轉換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage< 1){
intPage = 1;
}
}
Rs=connJB.executeQuery(sql);
Rs.absolute(1);

//獲取記錄總數
Rs.last();
intRowCount = Rs.getRow();

//記算總頁數
intPageCount = (intRowCount+intPageSize-1) / intPageSize;

//調整待顯示的頁碼
if(intPage >intPageCount) intPage = intPageCount;
if(intPageCount >0){
//將記錄指針定位到待顯示頁的第一條記錄上
Rs.absolute((intPage-1) * intPageSize+1);
//顯示數據
i = 0;
while(i< intPageSize && !Rs.isAfterLast()){
if(!className.equals("行業分類")){
//id=Rs.getInt("INFORMATION_ID");
strId=Rs.getString("INFORMATION_ID");
subjectName=Rs.getString("INFORMATION_NAME");
informationRank=Rs.getString("INFORMATION_RANK");

%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index2.jsp?INFORMATION_ID=<%=strId%>&INFORMATION_RANK=<%=informationRank%>" class="t8"><%=subjectName%></a></td>
</tr>
<% }else{
strId=Rs.getString("SUB_CLASS_VALUE");
subjectName=Rs.getString("SUB_CLASS_VALUE");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index_way.jsp?SMALL_CLASS=<%=strId%>" class="t8"><%=subjectName%></a></td>
</tr>
<%}%>

<%
Rs.next();
i++;

}Rs.close();
i=0;
%>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td valign="top" bgcolor="#F2F2F2">
<div align="left">

</div></td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C8C8C">
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">第<%=intPage%>頁 共<%//=intRowCount%>條記錄/共<%=intPageCount%>頁
<%
if(intPage<2){
out.print("首頁 | 上一頁");
}else{%>
<a href="index.jsp?CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">首頁 </a> | <a href="index.jsp?page=<%=intPage-1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">上一頁</a>
<%}%>
|
<%
if((intPageCount-intPage)<1)
{
out.print("下一頁 | 尾頁");
}else{%>
<a href="index.jsp?page=<%=intPage+1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">下一頁</a> | <a href="index.jsp?page=<%=intPageCount%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">尾頁</a>
<%}
}
else{
out.print("<center><table width=776 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF><center>目前還沒有內容!</center></td></tr></table></centre>");
}%>

int intPageSize; //一頁顯示的記錄數
int intRowCount; //記錄總數
int intPageCount; //總頁數
int intPage; //待顯示頁碼
String strPage;

//設置一頁顯示的記錄數
intPageSize = 25;

//取得待顯示頁碼
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁數據
intPage = 1;
}else{//將字元串轉換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage< 1){
intPage = 1;
}
}
Rs=connJB.executeQuery(sql);
Rs.absolute(1);

//獲取記錄總數
Rs.last();
intRowCount = Rs.getRow();

//記算總頁數
intPageCount = (intRowCount+intPageSize-1) / intPageSize;

//調整待顯示的頁碼
if(intPage >intPageCount) intPage = intPageCount;
if(intPageCount >0){
//將記錄指針定位到待顯示頁的第一條記錄上
Rs.absolute((intPage-1) * intPageSize+1);
//顯示數據
i = 0;
while(i< intPageSize && !Rs.isAfterLast()){
if(!className.equals("行業分類")){
//id=Rs.getInt("INFORMATION_ID");
strId=Rs.getString("INFORMATION_ID");
subjectName=Rs.getString("INFORMATION_NAME");
informationRank=Rs.getString("INFORMATION_RANK");

%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index2.jsp?INFORMATION_ID=<%=strId%>&INFORMATION_RANK=<%=informationRank%>" class="t8"><%=subjectName%></a></td>
</tr>
<% }else{
strId=Rs.getString("SUB_CLASS_VALUE");
subjectName=Rs.getString("SUB_CLASS_VALUE");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index_way.jsp?SMALL_CLASS=<%=strId%>" class="t8"><%=subjectName%></a></td>
</tr>
<%}%>

<%
Rs.next();
i++;

}Rs.close();
i=0;
%>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td valign="top" bgcolor="#F2F2F2">
<div align="left">

</div></td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C8C8C">
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">第<%=intPage%>頁 共<%//=intRowCount%>條記錄/共<%=intPageCount%>頁
<%
if(intPage<2){
out.print("首頁 | 上一頁");
}else{%>
<a href="index.jsp?CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">首頁 </a> | <a href="index.jsp?page=<%=intPage-1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">上一頁</a>
<%}%>
|
<%
if((intPageCount-intPage)<1)
{
out.print("下一頁 | 尾頁");
}else{%>
<a href="index.jsp?page=<%=intPage+1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">下一頁</a> | <a href="index.jsp?page=<%=intPageCount%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">尾頁</a>
<%}
}
else{
out.print("<center><table width=776 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF><center>目前還沒有內容!</center></td></tr></table></centre>");
}%>

『貳』 jsp查詢到的所有結果用一張表格顯示在一個頁面上,我想在表格下方實現上一頁,下一頁翻頁的功能按鈕

jsp頁面可以用分頁來控制查詢到的多條數據。
具體實例如下:
public class PageResult {
private List list; //裝分頁列表中的內容
private Integer firstPage; //第一頁
private Integer prePage; //上一頁
private Integer nextPage;//下一頁
private Integer currentPage;//當前頁面
private Integer totalPage; //總頁數/尾頁
private Integer count;//總條數
private Integer size; //每頁多少條
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Integer getFirstPage() {
return firstPage;
}
public void setFirstPage(Integer firstPage) {
this.firstPage = firstPage;
}
public Integer getPrePage() {
return (this.currentPage-1==0?1:this.currentPage-1);
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return (this.currentPage==this.totalPage?this.totalPage:this.currentPage+1);
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalPage() {
return (this.count%this.size==0?this.count/this.size:this.count/this.size+1);
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

public class listServlet extends HttpServlet {

private IProctDAO ;

@Override
public void init() throws ServletException {
= new IProctDAOImpl();
super.init();
}

<title>貨品列表</title>
</head>
<body>
<form action="/list" method="post">
<table border="1">
<tr>
<td>貨品名稱</td>
<td><input type="text" name="proctName"></td>
</tr>
<tr>
<td>品牌</td>
<td><input type="text" name="brand"></td>
</tr>
<tr>
<td>零售價</td>
<td><input type="text" name="salePrice"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="查詢"></td>
</tr>
</table>
</form>

<table border="1">
<tr>
<th>貨品</th>
<th>零售價</th>
<th>品牌</th>
<th>折扣</th>
<th>供應商</th>
<th>操作</th>
</tr>
<c:forEach items="${pageResult.list}" var="proct">
<tr>
<td>${proct.proctName}</td>
<td>${proct.salePrice}</td>
<td>${proct.brand}</td>
<td>${proct.cutoff}</td>
<td>${proct.supplier}</td>
<td><a href='/employee?cmd=remove&id=${employee.id}'>刪除</a> <a href='/employee?cmd=edit&id=${employee.id}'>修改</a></td>
</tr>
</c:forEach>
<tr>
<th><a href="/list?currentPage=${pageResult.firstPage}">首頁</a>${pageResult.firstPage}</th>
<th><a href="/list?currentPage=${pageResult.prePage}">上一頁</a>${pageResult.prePage}</th>
<th><a href="/list?currentPage=${pageResult.nextPage}">下一頁</a>${pageResult.nextPage}</th>
<th><a href="/list?currentPage=${pageResult.totalPage}">尾頁</a>${pageResult.totalPage}</th>
<th>當前${pageResult.currentPage}/${pageResult.totalPage}頁</th>
<th>總條數:${pageResult.count}</th>
</tr>
</table>

</body>
</html>

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String currentPage = request.getParameter("currentPage");

PageResult pageResult = new PageResult();

if(StringUtils.isBlank(currentPage))
{
currentPage="1";
}

pageResult.setCurrentPage(Integer.parseInt(currentPage));

pageResult.setSize(5);

List<Proct> list = .list(pageResult.getCurrentPage(), pageResult.getSize());

pageResult.setList(list);

int count = .count();

pageResult.setCount(count);

//如果當前頁等於第一頁,則上一頁就是第一頁,否則為當前頁上一頁
pageResult.setPrePage(pageResult.getPrePage());

//如果當前頁是尾頁,則下一頁就是尾頁,否則為當前頁的下一頁
pageResult.setNextPage(pageResult.getNextPage());

pageResult.setCount(count);

pageResult.setTotalPage(pageResult.getTotalPage());

request.setAttribute("pageResult", pageResult);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}

『叄』 JSP實現翻頁功能

第一個文件pagescount.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
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>pagescount</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">
-->

</head>

<body>
<%!
//設置每張網頁顯示三筆記錄(每頁顯示的記錄數)
int PageSize=3;

//設置欲顯示的頁數(初始頁)
int ShowPage=1;

//ResultSet的記錄筆數(總記錄數)
int RowCount=0;

//ResultSet分頁後的總數(總頁數)
int PageCount=0;

Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

%>
<%!
//連接資料庫並初始數據
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";

Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");

//將指標移至最後一條記錄
rs.last();

//獲取記錄總數

RowCount=rs.getRow();

//計算顯示的頁數(關鍵)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}

//執行關閉各個對象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>

第二個文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>分頁顯示</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">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");

//判斷是否取得ToPage參數
if(ToPage!=null)
{
//取得指定顯示的分頁頁數
ShowPage=Integer.parseInt(ToPage);

//下面的語句判斷用戶輸入的頁數是否正確
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}

//計算欲顯示頁的第一筆記錄位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>當前在第
<font size="4" color="red"><%=ShowPage %></font>頁,共<%=PageCount %>頁</h3>
<p>

<%
//利用for循環配合PageSize屬性取出一頁中的數據
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>書名ID</td>
<td><%=rs.getString("pub_id") %></td>

<td>書名</td>
<td><%=rs.getString("title") %></td>

<td>類型</td>
<td><%=rs.getString("type") %></td>

<td>價格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>備注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>

<%
//下面的語句用於輸出最後一條記錄時,將指針移到最後一筆記錄之後
if(!rs.next())
{
//跳出for循環
break;
}
}
%>
<table>
<tr>
<%
//判斷當前是否在第一頁,不是第一頁,則顯示到第一頁與下一頁的連接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一頁</a>
</td>
<%
}
//判斷是否在最後一頁,是,則顯示到最後一頁和下一頁
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最後一頁</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">頁
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>

『肆』 JSP 翻頁功能怎麼做

第一個文件pagescount.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
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>pagescount</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">
-->

</head>

<body>
<%!
//設置每張網頁顯示三筆記錄(每頁顯示的記錄數)
int PageSize=3;

//設置欲顯示的頁數(初始頁)
int ShowPage=1;

//ResultSet的記錄筆數(總記錄數)
int RowCount=0;

//ResultSet分頁後的總數(總頁數)
int PageCount=0;

Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

%>
<%!
//連接資料庫並初始數據
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";

Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");

//將指標移至最後一條記錄
rs.last();

//獲取記錄總數

RowCount=rs.getRow();

//計算顯示的頁數(關鍵)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}

//執行關閉各個對象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>

第二個文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>分頁顯示</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">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");

//判斷是否取得ToPage參數
if(ToPage!=null)
{
//取得指定顯示的分頁頁數
ShowPage=Integer.parseInt(ToPage);

//下面的語句判斷用戶輸入的頁數是否正確
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}

//計算欲顯示頁的第一筆記錄位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>當前在第
<font size="4" color="red"><%=ShowPage %></font>頁,共<%=PageCount %>頁</h3>
<p>

<%
//利用for循環配合PageSize屬性取出一頁中的數據
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>書名ID</td>
<td><%=rs.getString("pub_id") %></td>

<td>書名</td>
<td><%=rs.getString("title") %></td>

<td>類型</td>
<td><%=rs.getString("type") %></td>

<td>價格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>備注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>

<%
//下面的語句用於輸出最後一條記錄時,將指針移到最後一筆記錄之後
if(!rs.next())
{
//跳出for循環
break;
}
}
%>
<table>
<tr>
<%
//判斷當前是否在第一頁,不是第一頁,則顯示到第一頁與下一頁的連接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一頁</a>
</td>
<%
}
//判斷是否在最後一頁,是,則顯示到最後一頁和下一頁
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最後一頁</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">頁
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>

『伍』 JSP翻頁查詢

;%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
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>pagescount</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">
-->

</head>

<body>
<%!
//設置每張網頁顯示三筆記錄(每頁顯示的記錄數)
int PageSize=3;

//設置欲顯示的頁數(初始頁)
int ShowPage=1;

//ResultSet的記錄筆數(總記錄數)
int RowCount=0;

//ResultSet分頁後的總數(總頁數)
int PageCount=0;

Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

%>
<%!
//連接資料庫並初始數據
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";

Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");

//將指標移至最後一條記錄
rs.last();

//獲取記錄總數

RowCount=rs.getRow();

//計算顯示的頁數(關鍵)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}

//執行關閉各個對象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>

第二個文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>分頁顯示</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">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");

//判斷是否取得ToPage參數
if(ToPage!=null)
{
//取得指定顯示的分頁頁數
ShowPage=Integer.parseInt(ToPage);

//下面的語句判斷用戶輸入的頁數是否正確
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}

//計算欲顯示頁的第一筆記錄位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>當前在第
<font size="4" color="red"><%=ShowPage %></font>頁,共<%=PageCount %>頁</h3>
<p>

<%
//利用for循環配合PageSize屬性取出一頁中的數據
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>書名ID</td>
<td><%=rs.getString("pub_id") %></td>

<td>書名</td>
<td><%=rs.getString("title") %></td>

<td>類型</td>
<td><%=rs.getString("type") %></td>

<td>價格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>備注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>

<%
//下面的語句用於輸出最後一條記錄時,將指針移到最後一筆記錄之後
if(!rs.next())
{
//跳出for循環
break;
}
}
%>
<table>
<tr>
<%
//判斷當前是否在第一頁,不是第一頁,則顯示到第一頁與下一頁的連接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一頁</a>
</td>
<%
}
//判斷是否在最後一頁,是,則顯示到最後一頁和下一頁
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一頁</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最後一頁</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">頁
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>

閱讀全文

與jsp頁面翻頁相關的資料

熱點內容
安卓平板通用刷機包下載 瀏覽:751
安卓獲取內部存儲路徑 瀏覽:880
寫代碼兩台顯示器 瀏覽:327
unitypackage壓縮文件 瀏覽:493
奕心安卓 瀏覽:563
使用土地的有關證明文件包含哪些 瀏覽:493
數據標注哪裡可以接 瀏覽:482
在家自學編程下什麼學 瀏覽:705
最近很火的app軟體是什麼軟體 瀏覽:862
ai文字工具 瀏覽:157
蘭博玩游戲路徑怎麼選擇正確文件 瀏覽:972
淘寶直通車恢復老版本 瀏覽:510
播放草莓的圖片我都文件 瀏覽:55
微信大文件打不開 瀏覽:767
家裝合同准備哪些文件 瀏覽:296
應用bat合並excel文件 瀏覽:984
迅雷影音文件夾 瀏覽:109
makefile的文件路徑 瀏覽:392
計算機程序文件名擴展名為 瀏覽:982
網路游戲推廣策劃案 瀏覽:609

友情鏈接