首页 > 代码库 > 美化的bbs项目
美化的bbs项目
跟着教程做两天的bbs项目,终于好了,做了点笔记上传上来,哈哈(这里已经成了我的备忘录笔记本了啊)。
开发工具-----MyEclipse6.0:基于插件结构的开发工具,定义了许多接口。Eclipse默认只支持J2SE,lomboz-Eclipse和MyEclipse在Eclipse的基础上装了许多插件。
项目需要注理解的一些地方:
<%
String path=request.getContextPath(); //相当用于访问这个文件的路径,默认下也相当于文件名(如我们的第一个文件就是TestMyEclipse)
String basePath=request.getSchema()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //相当于http://127.0.0.1:8888/TestMyEclipse/
%>
html中的base标签:
<base href="http://www.mamicode.com/">
则其范围内的所有链接都是基于它的相对路径:
如: <a href="http://www.mamicode.com/aaaa.html">aaaa</a>
的实际路径为: http://127.0.0.1:8888/TestMyEclipse/aaaa.html
项目的主要过程:
<1.将静态页面改为动态页面:
<<1.首先可以上网找一些自己觉得做的好的页面下载下来,然后调整页面,删除不必要的东西(我下载下来的文件叫做article.htm)
<<2.讲article.htm转换为article.jsp(静态页面htm->动态页面jsp)
-----article.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*"%>
<%!
private void tree(List<Article> articles, Connection conn, int id, int grade) { //定义递归函数,以树状的形式显示帖子列表
String sql = "select * from article where pid = " + id; //找出所有父节点是id的节点
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt, sql);
try {
while(rs.next()) {
Article a = new Article();
a.initFromRs(rs);
a.setGrade(grade);
articles.add(a);
if(!a.isLeaf()) { //如果此节点不是叶子节点,调用函数,找出其子节点
tree(articles, conn, a.getId(), grade + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
}
}
%>
<%
List<Article> articles = new ArrayList<Article>(); //定义一个ArrayList有序存放Ariticle
Connection conn = DB.getConn();
tree(articles, conn, 0, 0);
DB.close(conn);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java语言*初级版</title>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/global.js"></script>
<link rel="alternate" type="application/rss+xml" title="RSS" href="">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/common.js"></script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57" width="100%">
</td>
<td width="1%"><img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-forumpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="98%"><p class="jive-breadcrumbs">论坛: Java语言*初级版
(模仿)</p>
<p class="jive-description"> 探讨Java语言基础知识,基本语法等 大家一起交流 共同提高!谢绝任何形式的广告 </p>
</td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="http://www.mamicode.com/post.jsp"><img src="http://www.mamicode.com/images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-post-thread" href="http://www.mamicode.com/post.jsp">发表新主题</a> <a href"></a></td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td><span class="nobreak"> 页:
1,316 - <span class="jive-paginator"> [ <a href="">上一页</a> | <a href="" class="">1</a> <a href="" class="jive-current">2</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0" class="">3</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=75&isBest=0" class="">4</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=100&isBest=0" class="">5</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=125&isBest=0" class="">6</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0">下一页</a> ] </span> </span> </td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div class="jive-thread-list">
<div class="jive-table">
<table summary="List of threads" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th class="jive-first" colspan="3"> 主题 </th>
<th class="jive-author"> <nobr> 作者
</nobr> </th>
<th class="jive-view-count"> <nobr> 浏览
</nobr> </th>
<th class="jive-msg-count" nowrap="nowrap"> 回复 </th>
<th class="jive-last" nowrap="nowrap"> 最新帖子 </th>
</tr>
</thead>
<tbody>
<%
for(Iterator<Article> it = articles.iterator(); it.hasNext(); ) { //iterator为迭代器,方便对集合中元素进行迭代,it.hasNext()为判断是否为最后一个元素
Article a = it.next();
String preStr = "";
for(int i=0; i<a.getGrade(); i++) {
preStr += "----"; //缩进的方式显示帖子列表
}
%>
<tr class="jive-even">
<td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="http://www.mamicode.com/images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap" width="1%">
<a href="http://www.mamicode.com/delete.jsp?id=&isLeaf=&pid=&rootId=">回复本主题</a> </td> //点击回复本主题转到reply.jsp,并将参数id和rootid传递给他
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div id="jive-message-holder">
<div class="jive-message-list">
<div class="jive-table">
<div class="jive-messagebox">
<table summary="Message" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr id="jive-message-780144" class="jive-odd" valign="top">
<td class="jive-first" width="1%">
<!-- 个人信息的table -->
<table border="0" cellpadding="0" cellspacing="0" width="150">
<tbody>
<tr>
<td><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td style="padding: 0px;" width="1%"><nobr> <a href="" title="诺曼底客">诺曼底客</a> </nobr> </td>
<td style="padding: 0px;" width="99%"><img class="jive-status-level-image" src="http://www.mamicode.com/images/level3.gif" title="世界新手" border="0"><br>
</td>
</tr>
</tbody>
</table>
<img class="jive-avatar" src="http://www.mamicode.com/images/avatar-display.png" border="0"> <br>
<br>
<span class="jive-description"> 发表:
34 <br>
点数: 100<br>
注册:
07-5-10 <br>
<a href="" target="_blank"><font color="red">访问我的Blog</font></a> </span> </td>
</tr>
</tbody>
</table>
<!--个人信息table结束-->
</td>
<td class="jive-last" width="99%"><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="1%"></td>
<td width="97%"><span class="jive-subject"> 父贴</span> </td>
<td class="jive-rating-buttons" nowrap="nowrap" width="1%"></td>
<td width="1%"><div class="jive-buttons">
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td> </td>
<td class="jive-icon"><a href="" title="回复本主题"><img src="http://www.mamicode.com/images/reply-16x16.gif" alt="回复本主题" border="0" height="16" width="16"></a> </td>
<td class="jive-icon-label"><a href="" title="回复本主题">回复</a> </td>
</tr>
</tbody>
</table>
</div></td>
</tr>
<tr>
<td colspan="4" style="border-top: 1px solid rgb(204, 204, 204);"><br>
<%=a.getCont() %> <br>
<br>
</td>
</tr>
<tr>
<td colspan="4" style="font-size: 9pt;"><img src="http://www.mamicode.com/images/sigline.gif"><br>
<font color="#568ac2">学程序是枯燥的事情,愿大家在一起能从中得到快乐!</font> <br>
</td>
</tr>
<tr>
<td colspan="4" style="border-top: 1px solid rgb(204, 204, 204); font-size: 9pt; table-layout: fixed;"> ·<a href="">http://www.bjsxt.com</font></a> </td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="jive-message-list-footer">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td nowrap="nowrap" width="1%"><br><br></td>
<td align="center" width="98%"><table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><a href=""><img src="http://www.mamicode.com/images/arrow-left-16x16.gif" alt="返回到主题列表" border="0" height="16" hspace="6" width="16"></a> </td>
<td><a href="">返回到主题列表</a> </td>
</tr>
</tbody>
</table></td>
<td nowrap="nowrap" width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</div></td>
<td width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<4.增加回复功能:
-----reply.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*" %>
<%
int id = Integer.parseInt(request.getParameter("id")); //接受article.detail传递过来的参数id和rootid
int rootId = Integer.parseInt(request.getParameter("rootId"));
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java|Java世界_中文论坛|ChinaJavaWorld技术论坛 : 初学java遇一难题!!望大家能帮忙一下 ...</title>
<meta http-equiv="content-type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/global.js"></script>
<!-- fckeditor -->
<script type="text/javascript" src="http://www.mamicode.com/fckeditor/fckeditor.js"></script>
<script type="text/javascript">
window.onload = function()
{
// Automatically calculates the editor base path based on the _samples directory.
// This is usefull only for these samples. A real application should use something like this:
// oFCKeditor.BasePath = ‘/fckeditor/‘ ; // ‘/fckeditor/‘ is the default value.
// var sBasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf(‘_samples‘)) ;
var sBasePath = "<%=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/fckeditor/" %>"
//alert(sBasePath);
var oFCKeditor = new FCKeditor( ‘cont‘ ) ;
oFCKeditor.BasePath = sBasePath ;
oFCKeditor.ReplaceTextarea() ;
}
</script>
<!-- end of fckeditor -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?threadID=744236">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57" width="100%">
</td>
<td width="1%"><img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-flatpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><p class="jive-breadcrumbs"> <a href="http://bbs.chinajavaworld.com/index.jspa">首页</a> » <a href="http://bbs.chinajavaworld.com/forumindex.jspa?categoryID=1">ChinaJavaWorld技术论坛|Java世界_中文论坛</a> » <a href="http://bbs.chinajavaworld.com/category.jspa?categoryID=2">Java 2 Platform, Standard Edition (J2SE)</a> » <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0">Java语言*初级版</a> </p>
<p class="jive-page-title"> 主题: reply </p></td>
<td width="1%"><div class="jive-accountbox"></div></td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="http://bbs.chinajavaworld.com/post%21reply.jspa?threadID=744236"><img src="http://www.mamicode.com/images/reply-16x16.gif" alt="回复本主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-reply-thread" href="http://www.mamicode.com/reply.jsp">回复本主题</a> </td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="930" height="61">
<tbody>
<tr valign="top">
<td width="99%"><div id="jive-message-holder">
<div class="jive-message-list">
<div class="jive-table">
<div class="jive-messagebox">
<form action="replyDeal.jsp" method="post"> //回复的界面,链接到replyDeal.jsp
<input type="hidden" name="pid" value="http://www.mamicode.com/"/>
<input type="hidden" name="rootId" value="http://www.mamicode.com/"/> //以隐藏域的方式传递pid和rootid给下一个页面
标题:<input type="text" name="title"><br>
内容:<textarea name="cont" rows="15" cols="80"></textarea>
<br>
<input type="submit" value="http://www.mamicode.com/submit"/>
</form>
</div>
</div>
</div>
<div class="jive-message-list-footer">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td nowrap="nowrap" width="1%"><br><br></td>
<td align="center" width="98%"><table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20"><img src="http://www.mamicode.com/images/arrow-left-16x16.gif" alt="返回到主题列表" border="0" height="16" hspace="6" width="16"></a> </td>
<td><a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20">返回到主题列表</a> </td>
</tr>
</tbody>
</table></td>
<td nowrap="nowrap" width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</div></td>
<td width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
-----replyDeal.jsp-----
<%@ page language="java" import="java.util.*, java.sql.*, com.bjsxt.bbs.*" pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("GBK");
int pid = Integer.parseInt(request.getParameter("pid")); //上一个页面的id是这一个页面的pid
int rootId = Integer.parseInt(request.getParameter("rootId"));
String title = request.getParameter("title");
System.out.println(title);
String cont = request.getParameter("cont");
System.out.println(cont);
Connection conn = DB.getConn();
boolean autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false); //设置同步性,避免出错
String sql = "insert into article values (null, ?, ?, ?, ?, now(), ?)"; //向数据库增加一条记录
PreparedStatement pstmt = DB.prepareStmt(conn, sql);
pstmt.setInt(1, pid);
pstmt.setInt(2, rootId);
pstmt.setString(3, title);
pstmt.setString(4, cont);
pstmt.setInt(5, 0);
pstmt.executeUpdate();
Statement stmt = DB.createStmt(conn);
stmt.executeUpdate("update article set isleaf = 1 where id = " + pid); //当此帖被回复是,变成非叶子节点
conn.commit();
conn.setAutoCommit(autoCommit);
DB.close(pstmt);
DB.close(stmt);
DB.close(conn);
%>
<span id="time" //指定id号,动态的拿到这个span style="background:red">5</span>秒钟后自动跳转,如果不跳转,请点击下面链接
<script language="JavaScript1.2" type="text/javascript">
<!--
// Place this in the ‘head‘ section of your page.
function delayURL(url) {
var delay = document.getElementById("time").innerHTML; //通过id号拿到里面的内容
//alert(delay);
if(delay > 0) { //当时间小于0的时候,转到主题帖
delay--;
document.getElementById("time").innerHTML = delay; //将delay设置到html的内容中
} else {
window.top.location.href = http://www.mamicode.com/url; > }
setTimeout("delayURL(‘" + url + "‘)", 1000); //每秒钟调用递归函数,使delay每一秒减1
}
//-->
</script>
<a href="http://www.mamicode.com/article.jsp">主题列表</a>
<script type="text/javascript">
delayURL("article.jsp");
</script>
<5.FCKeditor-----增加在线编辑器(javascript写的客户端控件)
<6.增加功能,回复成功后,5秒钟转到主题帖页面:
具体内容在上面replyDeal.jsp中。
Snippets:为收集进来的代码,想用随时加。
输出和删一段改一段是比较常用的调试方法。
<7.发表新主题贴:
-----post.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*,com.bjsxt.bbs.*"%>
<%
request.setCharacterEncoding("GBK");
String action = request.getParameter("action"); //拿到action
if (action != null && action.trim().equals("post")) { //判断是链接过来还是提交过来的
String title = request.getParameter("title"); //若是提交过来,则接受各个参数
System.out.println(title);
String cont = request.getParameter("cont");
System.out.println(cont);
Connection conn = DB.getConn();
boolean autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
int rootId = -1; //由于id是自动生成的,则利用Statement.RETURN_GENERATED_KEYS*****这个注意
String sql = "insert into article values (null, ?, ?, ?, ?, now(), ?)";
PreparedStatement pstmt = DB.prepareStmt(conn, sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, 0);
pstmt.setInt(2, rootId); //将自身的id设置为rootid
pstmt.setString(3, title);
pstmt.setString(4, cont);
pstmt.setInt(5, 0);
pstmt.executeUpdate();
ResultSet rsKey = pstmt.getGeneratedKeys();
rsKey.next();
rootId = rsKey.getInt(1);
Statement stmt = DB.createStmt(conn);
stmt.executeUpdate("update article set rootid = " + rootId + " where id = " + rootId);
conn.commit();
conn.setAutoCommit(autoCommit);
DB.close(pstmt);
DB.close(stmt);
DB.close(conn);
response.sendRedirect("article.jsp"); //完成对数据库的更新后,重新定向到article.jsp
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>发表新主题</title>
<meta http-equiv="content-type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css"
title="Integrated Styles">
<script language="JavaScript" type="text/javascript"
src="http://www.mamicode.com/images/global.js"></script>
<!-- fckeditor -->
<!-- 为了速度而没有使用fckeditor -->
<!-- end of fckeditor -->
<link rel="alternate" type="application/rss+xml" title="RSS"
href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?threadID=744236">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%">
<img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%">
<img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0">
</td>
</tr>
</tbody>
</table>
<br>
<div id="jive-flatpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%">
<p class="jive-breadcrumbs">
<a href="http://bbs.chinajavaworld.com/index.jspa">首页</a> »
<a
href="http://bbs.chinajavaworld.com/forumindex.jspa?categoryID=1">ChinaJavaWorld技术论坛|Java世界_中文论坛</a>
»
<a
href="http://bbs.chinajavaworld.com/category.jspa?categoryID=2">Java
2 Platform, Standard Edition (J2SE)</a> »
<a
href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0">Java语言*初级版</a>
</p>
<p class="jive-page-title">
发表新主题
<br>
</p>
</td>
<td width="1%">
<div class="jive-accountbox"></div>
</td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<br>
</div>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="930"
height="61">
<tbody>
<tr valign="top">
<td width="99%">
<div id="jive-message-holder">
<div class="jive-message-list">
<div class="jive-table">
<div class="jive-messagebox">
<form action="post.jsp" method="post"> //提交到页面自身,与reply.jsp不同
<input type="hidden" name="action" value="http://www.mamicode.com/post" /> //用隐藏域来区分是链接过来还是提交过来的
标题:
<input type="text" name="title">
<br>
内容:
<textarea name="cont" rows="15" cols="80"></textarea>
<br>
<input type="submit" value="http://www.mamicode.com/submit" />
</form>
</div>
</div>
</div>
<div class="jive-message-list-footer">
<br>
</div>
</div>
</td>
<td width="1%">
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<8.平板形式显示帖子:
-----articleFlat.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*"%>
<%
boolean logined = false; //logined用于表示是否已经登录
String adminLogined = (String)session.getAttribute("adminLogined"); //检查session,获得adminLogined的值,判断是否已经登录
if(adminLogined != null && adminLogined.trim().equals("true")) {
logined = true;
}
%>
<%
final int PAGE_SIZE = 4; //分页显示
int pageNo = 1;
String strPageNo = request.getParameter("pageNo");
if(strPageNo != null && !strPageNo.trim().equals("")) {
try {
pageNo = Integer.parseInt(strPageNo);
} catch (NumberFormatException e) {
pageNo = 1;
}
}
if(pageNo <= 0) pageNo = 1;
int totalPages = 0;
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
Statement stmtCount = DB.createStmt(conn);
ResultSet rsCount = DB.executeQuery(stmtCount, "select count(*) from article where pid = 0"); //拿出所有主题帖
rsCount.next();
int totalRecords = rsCount.getInt(1);
totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE;
if(pageNo > totalPages) pageNo = totalPages;
Statement stmt = DB.createStmt(conn);
int startPos = (pageNo-1) * PAGE_SIZE;
String sql = "select * from article where pid = 0 order by pdate desc limit " + startPos + "," + PAGE_SIZE ; //取出想要显示的那一页的数据
System.out.println(sql);
ResultSet rs = DB.executeQuery(stmt, sql);
while(rs.next()) {
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
}
DB.close(rsCount);
DB.close(stmtCount);
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java语言*初级版</title>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/global.js"></script>
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?forumID=20">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/common.js"></script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57" width="100%">
</td>
<td width="1%"><img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-forumpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="98%"><p class="jive-breadcrumbs">论坛: Java语言*初级版
(模仿)</p>
<p class="jive-description"> 探讨Java语言基础知识,基本语法等 大家一起交流 共同提高!谢绝任何形式的广告 </p>
</td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="http://www.mamicode.com/post.jsp"><img src="http://www.mamicode.com/images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-post-thread" href="http://www.mamicode.com/post.jsp">发表新主题</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&isBest=1"></a></td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td><span class="nobreak"> 页:
第<%=pageNo %>页,共页 - <span class="jive-paginator"> [</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/articleFlat.jsp?pageNo=1">第一页</a></span></span>
<span class="nobreak"><span class="jive-paginator">|</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/articleFlat.jsp?pageNo=">上一页</a>
</span></span>
<span class="nobreak"><span class="jive-paginator">| </span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/articleFlat.jsp?pageNo=">下一页</a>
|
<a href="http://www.mamicode.com/articleFlat.jsp?pageNo=">MOD</a> //增加更新的链接
<a href="http://www.mamicode.com/delete.jsp?id=&isLeaf=&pid="><%=a.getTitle() %></a></td>
<td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226030">bjsxt</a> </span></td>
<td class="jive-view-count" width="1%"> 10000</td>
<td class="jive-msg-count" width="1%"> 0</td>
<td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate()) %> <br>
by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780182#780182" title="jingjiangjun" style="">bjsxt »</a> </div></td>
</tr>
<%
lineNo++;
}
%>
</tbody>
</table>
</div>
</div>
<div class="jive-legend"></div></td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
</body>
</html>
-----articleDetailFlat.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*" %>
<%
String strId = request.getParameter("id");
if(strId == null || strId.trim().equals("")) {
out.println("Error ID!");
return;
}
int id = 0;
try {
id = Integer.parseInt(strId);
} catch (NumberFormatException e) {
out.println("Error ID Again!");
return;
}
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
String sql = "select * from article where rootid = " + id + " order by pdate asc"; //拿出id的帖子的所有子贴
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt, sql);
while(rs.next()) {
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
}
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java|Java世界_中文论坛|ChinaJavaWorld技术论坛 : 初学java遇一难题!!望大家能帮忙一下 ...</title>
<meta http-equiv="content-type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/global.js"></script>
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?threadID=744236">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57" width="100%">
</td>
<td width="1%"><img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-flatpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><p class="jive-breadcrumbs"> <a href="http://bbs.chinajavaworld.com/index.jspa">首页</a> » <a href="http://bbs.chinajavaworld.com/forumindex.jspa?categoryID=1">ChinaJavaWorld技术论坛|Java世界_中文论坛</a> » <a href="http://bbs.chinajavaworld.com/category.jspa?categoryID=2">Java 2 Platform, Standard Edition (J2SE)</a> » <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0">Java语言*初级版</a> </p>
<p class="jive-page-title"> 主题 </p></td>
<td width="1%"><div class="jive-accountbox"></div></td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="http://bbs.chinajavaworld.com/post%21reply.jspa?threadID=744236"><img src="http://www.mamicode.com/images/reply-16x16.gif" alt="回复本主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-reply-thread" href="">回复本主题</a> </td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div id="jive-message-holder">
<div class="jive-message-list">
<div class="jive-table">
<div class="jive-messagebox">
<!-- start -->
<%
for(int i=0; i<articles.size(); i++) { //循环显示所有子帖
Article a = articles.get(i);
String floor = i == 0 ? "楼主" : "第" + i + "楼"; //显示楼数
%>
<table summary="Message" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr id="jive-message-780144" class="jive-odd" valign="top">
<td class="jive-first" width="1%">
<!-- 个人信息的table -->
<table border="0" cellpadding="0" cellspacing="0" width="150">
<tbody>
<tr>
<td><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td style="padding: 0px;" width="1%"><nobr> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=215489" title="诺曼底客">诺曼底客</a> </nobr> </td>
<td style="padding: 0px;" width="99%"><img class="jive-status-level-image" src="http://www.mamicode.com/images/level3.gif" title="世界新手" border="0"><br>
</td>
</tr>
</tbody>
</table>
<img class="jive-avatar" src="http://www.mamicode.com/images/avatar-display.png" border="0"> <br>
<br>
<span class="jive-description"> 发表:
34 <br>
点数: 100<br>
注册:
07-5-10 <br>
<a href="http://blog.chinajavaworld.com/u/215489" target="_blank"><font color="red">访问我的Blog</font></a> </span> </td>
</tr>
</tbody>
</table>
<!--个人信息table结束-->
</td>
<td class="jive-last" width="99%"><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="1%"></td>
<td width="97%"><span class="jive-subject"><%=floor + "-------" + a.getTitle()%></span> </td>
<td class="jive-rating-buttons" nowrap="nowrap" width="1%"></td>
<td width="1%"><div class="jive-buttons">
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td> </td>
<td class="jive-icon"><a href="http://bbs.chinajavaworld.com/post%21reply.jspa?messageID=780144" title="回复本主题"><img src="http://www.mamicode.com/images/reply-16x16.gif" alt="回复本主题" border="0" height="16" width="16"></a> </td>
<td class="jive-icon-label"><a href="http://www.mamicode.com/reply.jsp?id=&rootId=" title="回复本主题">回复</a> </td>
</tr>
</tbody>
</table>
</div></td>
</tr>
<tr>
<td colspan="4" style="border-top: 1px solid rgb(204, 204, 204);"><br>
<%=a.getCont() %> <br>
<br>
</td>
</tr>
<tr>
<td colspan="4" style="font-size: 9pt;"><img src="http://www.mamicode.com/images/sigline.gif"><br>
<font color="#568ac2">学程序是枯燥的事情,愿大家在一起能从中得到快乐!</font> <br>
</td>
</tr>
<tr>
<td colspan="4" style="border-top: 1px solid rgb(204, 204, 204); font-size: 9pt; table-layout: fixed;"> ·<a href="http://www.bjsxt.com"><font color="#666666">http://www.bjsxt.com</font></a> </td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
<!-- end -->
<%
}
%>
</div>
</div>
</div>
<div class="jive-message-list-footer">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td nowrap="nowrap" width="1%"><br><br></td>
<td align="center" width="98%"><table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20"><img src="http://www.mamicode.com/images/arrow-left-16x16.gif" alt="返回到主题列表" border="0" height="16" hspace="6" width="16"></a> </td>
<td><a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20">返回到主题列表</a> </td>
</tr>
</tbody>
</table></td>
<td nowrap="nowrap" width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</div></td>
<td width="1%"> </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<9.增加管理员管理功能(如:删除,登录):
-----delete.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*"%>
<%@ include file="_SessionCheck.jsp" %> //包含_SessionCheck.jsp判断请求页面delete.jsp时是否已经登录
<%!
private void delete(Connection conn, int id, boolean isLeaf) { //利用递归函数删除帖子及其子贴
//delete all the children
//delete(conn, chids‘s id)
if(!isLeaf) { //判断其是否为叶子节点
String sql = "select * from article where pid = " + id;
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt, sql);
try {
while(rs.next()) {
delete(conn, rs.getInt("id"), rs.getInt("isleaf") == 0);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
}
}
//delete self
DB.executeUpdate(conn, "delete from article where id = " + id); //是叶子节点直接删除自己就可以
}
%>
<%
int id = Integer.parseInt(request.getParameter("id")); //接受参数
int pid = Integer.parseInt(request.getParameter("pid"));
boolean isLeaf = Boolean.parseBoolean(request.getParameter("isLeaf"));
String url = request.getParameter("from");
System.out.println("url = " + url);
Connection conn = null;
boolean autoCommit = true;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DB.getConn();
autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
delete(conn, id, isLeaf); //调用函数
stmt = DB.createStmt(conn);
rs = DB.executeQuery(stmt, "select count(*) from article where pid = " + pid);
rs.next();
int count = rs.getInt(1);
if(count <= 0) { //判断他是否还有子节点,若没有则将其改为子节点
DB.executeUpdate(conn, "update article set isleaf = 0 where id = " + pid);
}
conn.commit();
} finally {
conn.setAutoCommit(autoCommit);
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
response.sendRedirect(url); //转回到被点击删除按钮的那个页面
%>
-----login.jsp----- //管理员登录界面
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*,com.bjsxt.bbs.*"%>
<%
request.setCharacterEncoding("GBK");
String action = request.getParameter("action"); //接收参数
String username = "";
if (action != null && action.trim().equals("login")) {
username = request.getParameter("username"); //接收参数
//check username whether valid or not!
String password = request.getParameter("password"); //接受参数
if(username == null || !username.trim().equals("admin")) { //判断用户名是否正确
out.println("username not correct!");
} else if(password == null || !password.trim().equals("admin")) { //判断密码是否正确
out.println("password not correct!");
} else {
session.setAttribute("adminLogined" , "true"); //在session中放入adminLogined,值为true,代表已经登录,在其他页面取这个值,就可以知道是否已经登录
response.sendRedirect("articleFlat.jsp"); //登录成功后转到articleFlat.jsp页面
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>管理员登录</title>
</head>
<body>
<form action="login.jsp" method="post"> //提交到自身,提交密码使用post方式
<input type="hidden" name="action" value="http://www.mamicode.com/login"/> //使用隐藏域区分是自身还是点删除时转到此页面的
用户名:
<input type="text" name="username" value="http://www.mamicode.com/"/> //value="http://www.mamicode.com/"> //将取出的帖子的标题显示出来
<br>
内容:
<textarea name="cont" rows="15" cols="80"><%=a.getCont() %></textarea> //将取出的帖子的内容显示出来
<br>
<input type="submit" value="http://www.mamicode.com/submit" />
</form>
</div>
</div>
</div>
<div class="jive-message-list-footer">
<br>
</div>
</div>
</td>
<td width="1%">
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<12.项目总结:
学习一个web项目是怎么样的,为下一个项目做准备。
掌握的技巧,通过页面往数据库中增加,删除,修改数据的方法。
增加搜索功能:
-----searchResult.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*"%>
<%
//request.setCharacterEncoding("GB18030");
boolean logined = false;
String adminLogined = (String)session.getAttribute("adminLogined");
if(adminLogined != null && adminLogined.trim().equals("true")) {
logined = true;
}
%>
<%
String keyword = new String(request.getParameter("keyword").getBytes("8859_1"), "GBK"); //通过http协议传递过来的中文都是以8859_1编码的
if(keyword == null) keyword = "";
final int PAGE_SIZE = 4;
int pageNo = 1;
String strPageNo = request.getParameter("pageNo");
if(strPageNo != null && !strPageNo.trim().equals("")) {
try {
pageNo = Integer.parseInt(strPageNo);
} catch (NumberFormatException e) {
pageNo = 1;
}
}
int totalPages = 0;
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
Statement stmtCount = DB.createStmt(conn);
String sqlCount = "select count(*) from article where title like ‘%" + keyword
+ "%‘ or cont like ‘%" + keyword + "%‘";
System.out.println(sqlCount);
ResultSet rsCount = DB.executeQuery(stmtCount, sqlCount);
rsCount.next();
int totalRecords = rsCount.getInt(1);
totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE;
if(pageNo > totalPages) pageNo = totalPages;
if(pageNo <= 0) pageNo = 1;
Statement stmt = DB.createStmt(conn);
int startPos = (pageNo-1) * PAGE_SIZE;
String sql = "select * from article where title like ‘%" + keyword
+ "%‘ or cont like ‘%" + keyword + "%‘ order by pdate desc limit " + startPos + "," + PAGE_SIZE ; //从数据库中搜索出所有要的记录
System.out.println(sql);
ResultSet rs = DB.executeQuery(stmt, sql);
while(rs.next()) {
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
}
DB.close(rsCount);
DB.close(stmtCount);
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java语言*初级版</title>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/global.js"></script>
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?forumID=20">
<script language="JavaScript" type="text/javascript" src="http://www.mamicode.com/images/common.js"></script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img src="http://www.mamicode.com/images/header-stretch.gif" border="0" height="57" width="100%">
</td>
<td width="1%"><img src="http://www.mamicode.com/images/header-right.gif" height="57" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-forumpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="98%"><p class="jive-breadcrumbs">论坛: Java语言*初级版
(模仿)</p>
<p class="jive-description"> 探讨Java语言基础知识,基本语法等 大家一起交流 共同提高!谢绝任何形式的广告 </p>
</td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="http://www.mamicode.com/post.jsp"><img src="http://www.mamicode.com/images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-post-thread" href="http://www.mamicode.com/post.jsp">发表新主题</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&isBest=1"></a></td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td><span class="nobreak"> 页:
第<%=pageNo %>页,共页 - <span class="jive-paginator"> [</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/articleFlat.jsp?pageNo=1">第一页</a></span></span>
<span class="nobreak"><span class="jive-paginator">|</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/searchResult.jsp?pageNo=">上一页</a>
</span></span>
<span class="nobreak"><span class="jive-paginator">| </span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="http://www.mamicode.com/searchResult.jsp?pageNo=&keyword=">最末页</a> ] </span> </span> </td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div class="jive-thread-list">
<div class="jive-table">
<table summary="List of threads" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th class="jive-first" colspan="3"> 主题 </th>
<th class="jive-author"> <nobr> 作者
</nobr> </th>
<th class="jive-view-count"> <nobr> 浏览
</nobr> </th>
<th class="jive-msg-count" nowrap="nowrap"> 回复 </th>
<th class="jive-last" nowrap="nowrap"> 最新帖子 </th>
</tr>
</thead>
<tbody>
<%
int lineNo = 0;
for(Iterator<Article> it = articles.iterator(); it.hasNext(); ) {
Article a = it.next();
String classStr = lineNo%2 == 0 ? "jive-even" : "jive-odd";
%>
<tr class="<%=classStr %>">
<td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="http://www.mamicode.com/images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap" width="1%">
<%
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
url += request.getContextPath();
url += request.getServletPath();
url += request.getQueryString() == null ? "" : ("?" + request.getQueryString());
//System.out.println(url);
//System.out.println(request.getRequestURI());
//System.out.println(request.getRequestURL());
%>
<%if (logined) {%>
<a href="http://www.mamicode.com/modify.jsp?id=">MOD</a>
<a href="http://www.mamicode.com/delete.jsp?id=&isLeaf=&pid="><%=a.getTitle() %></a></td>
<td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226030">bjsxt</a> </span></td>
<td class="jive-view-count" width="1%"> 10000</td>
<td class="jive-msg-count" width="1%"> 0</td>
<td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate()) %> <br>
by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780182#780182" title="jingjiangjun" style="">bjsxt »</a> </div></td>
</tr>
<%
lineNo++;
}
%>
</tbody>
</table>
</div>
</div>
<div class="jive-legend"></div></td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
</body>
</html>