jsp新闻内容分页

<%@ page language="java" import="java.sql.*,java.io.*" 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>jsp新闻内容分页</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">
</head>

<body>

<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:mydb","test","test");
ps = conn.prepareStatement("select content from test where id=1");
rs = ps.executeQuery();
String content =null;
while(rs.next()){
content = rs.getString("content");
}

int numStr =5; //每页显示多少字符
int countStr; //一共有多少字符数
int intPage=-1; //待显示页码
int intPageCount; //总页数

countStr =content.length();
if(countStr%numStr==0)
{
intPageCount = countStr / numStr ;//共几页
}else{
intPageCount = countStr / numStr +1;
}

%>
<%
rs.close();
ps.close();
conn.close();
%>
<%
if(request.getParameter("pages")==null)
intPage=1;
else
intPage=Integer.parseInt(request.getParameter("pages"));
int startNumStr=(intPage-1)*numStr;//当前页开始字符数位置
int endNumStr=(intPage)*numStr;//当前页结尾字符数位置

while(startNumStr<endNumStr)
{
if((intPage-1)< countStr / numStr){
out.print(content.substring((intPage-1)*numStr,(intPage)*numStr));
}else{
out.print(content.substring((intPage-1)*numStr,countStr));
}
startNumStr=startNumStr+numStr;
}
%>

<br />
<table width="80%" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="60%" >
<%
if(countStr>numStr){
%>
共<%=intPageCount %>,当前第<%=intPage%>页  
<a href="MyJsp1.jsp?pages=1" >首页</a>

<%
if((intPage-1)<1)
out.print("上一页");
else
out.print("<a href='MyJsp1.jsp?pages="+(intPage-1)+"''>上一页</a>");
%>

<%
if(intPage<intPageCount)
out.print("<a href='MyJsp1.jsp?pages="+(intPage+1)+"''>下一页</a>");
else
out.print("下一页");
%>
 <a href='MyJsp1.jsp?pages=<%=intPageCount%>''> 尾页</a>
<%
}
%>
</td>
</tr>
</table>

</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值