📄 bbslist.jsp
字号:
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="BeanLink.DBBean"%>
<jsp:useBean id="dBean" class="BeanLink.DBBean" scope="session"/>
<html>
<head>
<title>论坛列表</title>
</head>
<body>
<CENTER>
<br>
<%
String loginSign = (String)session.getValue("loginSign");
String username = (String)session.getValue("username");//作者名
if(loginSign!=null&&loginSign.equals("OK"))
{//显示文章列表
out.println("<font size='6' color='blue'>文章列表</font>");
}
else
{//无此权限,定向到用户登录页面
response.sendRedirect("../index.jsp");
}
%>
</CENTER>
<%
//变量初始化
int pageLine = 2;//每页记录数
int totalRec = 0;//数据集的总记录数
int intPageCount = 0;//总页数
int intPage = 0;//当前页号
%>
<%
String sdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sconn="jdbc:odbc:mydb";
String suser="";
String spass="";
String tablename="board";
dBean.openConn(sdriver,sconn,suser,spass);
ResultSet rs=null;
String sql = "";
//--------增加查询需要使用的字符串变量的定义
String searchType = request.getParameter("searchType");
String searchContent = request.getParameter("searchContent");
if(searchType!=null)//确保不为空以后才做编码转换
searchContent = new String(searchContent.getBytes("8859_1"));
//取得总记录数------增加查询功能后sql语句的修改
if(searchType==null)//表示没有提交查询表单
sql="select count(*) as cnt from "+tablename+" where parent_no=0";
else//查询表单提交,则取值
{
if(searchType.equals("byTitle"))
sql="select count(*) as cnt from "+tablename+" where parent_no=0 and title like'%"+searchContent+"%'";
else
sql="select count(*) as cnt from "+tablename+" where parent_no=0 and speaker like'%"+searchContent+"%'";
}
System.out.println(sql);
rs=dBean.executeQuery(sql);
if(rs!=null&&rs.next())
totalRec = rs.getInt("cnt");
if(rs!=null)
{
rs.close();
rs = null;
}
//计算总页数
if(totalRec==0)
intPageCount = 0;
else
intPageCount = (totalRec-1)/pageLine + 1;
if(intPageCount>0)
{//总页数大于0--if程序断开始
//page是链接参数或者控件名称,表示当前页号,赋值给intPage
if(request.getParameter("page")!=null)
intPage = Integer.parseInt(request.getParameter("page"));
if(intPage>intPageCount)
intPage=intPageCount;//如果当前页数大于总页数,则当前页数等于总页数
if(intPage<1)
intPage=1;//如果当前页数小于1,则把它置为1
//显示记录内容,再做一次查询------增加查询功能后sql语句的修改
if(searchType==null)//表示没有提交查询表单
sql="select * from "+tablename+" where parent_no=0 order by provide_time desc";
else//查询表单提交,则取值
{
if(searchType.equals("byTitle"))
sql="select * from "+tablename+" where parent_no=0 and title like'%"+searchContent+"%' order by provide_time desc";
else
sql="select * from "+tablename+" where parent_no=0 and speaker like'%"+searchContent+"%' order by provide_time desc";
}
rs=dBean.executeQuery(sql);
System.out.println(sql);
%>
<table border="1" width="80%" align="center" bgcolor="#ccffff" height="38">
<tr>
<td width="10%" height="10">
<p align="center">表情</p></td>
<td width="35%" height="10">
<p align="center">标题</p></td>
<td width="15%" height="10">
<p align="center">发表人</p></td>
<td width="20%" height="10">
<p align="center">发表时间</p></td>
<td width="10%" height="10">
<p align="center">回复数</p></td>
<td width="10%" height="10">
<p align="center">点击数</p></td>
</tr>
<%
//记录指针走过当前页以前所有页的记录
for(int i=1; i<=(intPage-1)*pageLine; i++)
rs.next();
for(int i=1; i<=pageLine; i++)
{//显示当前页的记录--for开始
if(rs.next())
{
long serialNo = rs.getLong("serialNo");
String title = rs.getString("title");
String speaker = rs.getString("speaker");
java.util.Date provide_time = rs.getDate("provide_time");
long reply_num = rs.getLong("reply_num");
long click_num = rs.getLong("click_num");
String emote = rs.getString("emote");
emote = "../../images/"+emote+".gif";
%>
<tr>
<td width="10%" bgcolor="#ffffcc" height="10">
<p align="center"><image src="<%=emote%>" ></p></td>
<td width="35%" bgcolor="#ffffcc" height="10">
<p align="center"><a href="bbslistmore.jsp?serialNo=<%=serialNo%>"><%=title%></a></p></td>
<td width="15%" bgcolor="#ffffcc" height="10">
<p align="center"><%=speaker%></p></td>
<td width="20%" bgcolor="#ffffcc" height="10">
<p align="center"><%=provide_time%></p></td>
<td width="10%" bgcolor="#ffffcc" height="10">
<p align="center"><%=reply_num%></p></td>
<td width="10%" bgcolor="#ffffcc" height="10">
<p align="center"><%=click_num%></p></td>
</tr>
<%
}
}//显示当前页的记录--for结束
%>
</table>
<%
}//总页数大于0--if结束
//关闭对象操作
if(rs!=null)
{
rs.close();
rs = null;
}
dBean.closeConn();
%>
<%
//以下用于分页显示
out.println("<center>");
//form表单开始,注意action为当前页面
//--------action的参数同下面的链接处理,只是page不用做参数了,因为page是text控件的名字,与参数一样会保存在request对象中
if(searchType==null)
{
out.println("<form method='post' action='bbslist.jsp'>");
}
else
{
out.println("<form method='post' action='bbslist.jsp?searchType="+searchType+"&searchContent="+searchContent+"'>");
}
if(intPage<2)//如果当前页数小于2,则首页和上一页无链接
out.print("<font color='#999966'>首页 上一页</font> ");
else
//注意href链接中用?传递参数,多个参数用&符号隔开,其中参数的名字为page
//可以通过request.getParameter("page")得到
//首页和下一页的链接增加查询类别和内容参数,以便bbslist.jsp从头处理时,仍然按查询处理
{
if(searchType==null)
{
out.print("<a href='bbslist.jsp?page=1'>首页</a> ");
out.print("<a href='bbslist.jsp?page="+(intPage-1)+"'>上一页</a> ");
}
else
{
out.print("<a href='bbslist.jsp?page=1&searchType="+searchType+"&searchContent="+searchContent+"'>首页</a> ");
out.print("<a href='bbslist.jsp?page="+(intPage-1)+"&searchType="+searchType+"&searchContent="+searchContent+"'>上一页</a> ");
}
}
if(intPage-intPageCount>=0)//如果当前页数小于总页数,则下一页和尾页有链接
out.print("<font color='#999966'>下一页 尾页</font> ");
else
//参数同首页和下一页的链接处理
{
if(searchType==null)
{
out.print("<a href='bbslist.jsp?page="+(intPage+1)+"'>下一页</a> ");
out.print("<a href='bbslist.jsp?page="+intPageCount+"'>尾页</a> ");
}
else
{
out.print("<a href='bbslist.jsp?page="+(intPage+1)+"&searchType="+searchType+"&searchContent="+searchContent+"'>下一页</a> ");
out.print("<a href='bbslist.jsp?page="+intPageCount+"&searchType="+searchType+"&searchContent="+searchContent+"'>尾页</a> ");
}
}
//显示页次:当前页数/总页数/总记录数/每页记录数
out.print(" 页次:<strong><font color=red>"+intPage+"</font>/"+intPageCount+" 总记录数/"+totalRec+" 每页记录数/"+pageLine+"</strong>");
//注意:input text控件的name为page
//与前面链接中的参数同名,以便通过request.getParameter("page")得到
out.print(" 转到第<input type='text' name='page' size=4 value="+intPage+">页");
out.print("<input type='submit' value='GO' >");
out.println("</form>");
//form表单结束
out.println("</center>");
%>
<center>
<P>
<font size="6" color="green">发表新贴</font>
<form method="post" action ="bbsdealaddnew.jsp">
<P>发帖人:<%=username%></P>
<p>主 题:<input type="text" name="title" size="70"></p>
<p>表 情:
<%
for(int i=0; i<10; i++)
{
String imageNo = "0"+Integer.toString(i);
String imageAddress = "../../images/"+imageNo+".gif";
if(i==0)
out.println("<input type='radio' value="+imageNo+" name='emote' checked><image src="+imageAddress+"> ");
else
out.println("<input type='radio' value="+imageNo+" name='emote'><image src="+imageAddress+"> ");
}
%>
</p>
<p>内 容:</p>
<p><textarea rows="8" name="content" cols="80"></textarea></p>
<p><input type="submit" value="提交" name="B1">
<input type="reset" value="重写" name="B2"></p>
</form>
</P>
</center>
<center>
<P>
<font size="6" color="green">贴子查询</font>
<form method="post" action ="bbslist.jsp">
<p><select name="searchType" size="1">
<option value="byTitle" selected>按标题</option>
<option value="bySpeaker" >按作者</option>
</select></p>
<p><input type="text" name="searchContent" value=""></p>
<p><input type="submit" value="提交" name="B3">
<input type="reset" value="重写" name="B4"></p>
</form>
</P>
</center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -