跟着教程做两天的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="<%=basePath%>">
则其范围内的所有链接都是基于它的相对路径:
如: <a
href="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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="images/global.js"></script>
<link rel="alternate"
type="application/rss+xml" title="RSS" href="">
<script
language="JavaScript" type="text/javascript"
src="images/common.js"></script>
</head>
<body>
<table
border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img
src="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="post.jsp"><img
src="images/post-16x16.gif" alt="发表新主题" border="0" height="16"
width="16"></a></td>
<td
class="jive-icon-label"><a id="jive-post-thread"
href="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="images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap"
width="1%">
<a
href="delete.jsp?id=<%=a.getId()%>&isLeaf=<%=a.isLeaf()%>&pid=<%=a.getPid()
%>">DEL</a> //当点击DEL时转到Delete.jsp进行登录操作
</td>
<td class="jive-thread-name" width="95%"><a
id="jive-thread-1" href="articleDetail.jsp?id=<%=a.getId()
%>"><%=preStr + a.getTitle() %></a></td>
//显示帖子的标题
<td class="jive-author" nowrap="nowrap" width="1%"><span
class=""> <a href="">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="" title="jingjiangjun" style="">bjsxt »</a>
</div></td>
</tr>
<%--
<tr class="jive-odd">
<td
class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet">
<img src="images/read-16x16.gif" alt="已读" border="0" height="16"
width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap"
width="1%"> </td>
<td
class="jive-thread-name" width="95%"><a id="jive-thread-2"
href="">请兄弟们指点下那里错误,,,</a></td>
<td
class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a
href="">403783154</a> </span></td>
<td class="jive-view-count" width="1%"> 52</td>
<td class="jive-msg-count" width="1%"> 2</td>
<td class="jive-last" nowrap="nowrap" width="1%"><div
class="jive-last-post"> 2007-9-13 上午8:40 <br>
by: <a href="" title="downing114" style="">downing114
»</a> </div></td>
</tr>
--%>
<%
}
%>
</tbody>
</table>
</div>
</div>
<div class="jive-legend"></div></td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
</body>
</html>
<2.树状结构展示帖子
<<1.用JavaBean封装:
------DB.java-----
package com.bjsxt.bbs;
import java.sql.Connection;
import java.sql.DriverManager;
import
java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
public class DB { //定义DB来封装所有有关数据库的操作
public static Connection getConn()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root" ,
"root");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public
static Statement createStmt(Connection conn) {
Statement stmt =
null;
try {
stmt = conn.createStatement();
} catch (SQLException
e) {
e.printStackTrace();
}
return stmt;
}
public
static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs =
null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException
e) {
e.printStackTrace();
}
return rs;
}
public
static int executeUpdate(Connection conn, String sql) {
int ret =
0;
Statement stmt = null;
try {
stmt =
conn.createStatement();
ret = stmt.executeUpdate(sql);
} catch
(SQLException e) {
e.printStackTrace();
} finally
{
close(stmt);
}
return ret;
}
public static
PreparedStatement prepareStmt(Connection conn, String sql)
{
PreparedStatement pstmt = null;
try {
pstmt =
conn.prepareStatement(sql);
} catch (SQLException e)
{
e.printStackTrace();
}
return pstmt;
}
public
static PreparedStatement prepareStmt(Connection conn, String sql, int
autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
pstmt
= conn.prepareStatement(sql, autoGeneratedKeys);
} catch (SQLException e)
{
e.printStackTrace();
}
return pstmt;
}
public
static void close(Connection conn) {
if(conn != null) {
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
conn =
null;
}
}
public static void close(Statement stmt)
{
if(stmt != null) {
try {
stmt.close();
} catch
(SQLException e) {
e.printStackTrace();
}
stmt =
null;
}
}
public static void close(ResultSet rs) {
if(rs
!= null) {
try {
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
rs = null;
}
}
}
-----Article.java-----
package com.bjsxt.bbs;
import java.sql.ResultSet;
import java.sql.SQLException;
import
java.util.Date;
public class Article {
//定义Ariticle来封装所有从数据库中去来的数据,并配以getter,settter
private int id;
private
int pid;
private int rootId;
private String title;
private String
cont;
private Date pdate;
private boolean isLeaf;
private int
grade;
public int getId() {
return id;
}
public void setId(int
id) {
this.id = id;
}
public int getPid() {
return
pid;
}
public void setPid(int pid) {
this.pid =
pid;
}
public int getRootId() {
return rootId;
}
public
void setRootId(int rootId) {
this.rootId = rootId;
}
public String
getTitle() {
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getCont() {
return
cont;
}
public void setCont(String cont) {
this.cont =
cont;
}
public Date getPdate() {
return pdate;
}
public
void setPdate(Date pdate) {
this.pdate = pdate;
}
public boolean
isLeaf() {
return isLeaf;
}
public void setLeaf(boolean isLeaf)
{
this.isLeaf = isLeaf;
}
public int getGrade() {
return
grade;
}
public void setGrade(int grade) {
this.grade =
grade;
}
public void initFromRs(ResultSet rs) {
//初始化Article
try
{
setId(rs.getInt("id"));
setPid(rs.getInt("pid"));
setRootId(rs.getInt("rootid"));
setTitle(rs.getString("title"));
setLeaf(rs.getInt("isleaf")
== 0 ? true :
false);
setPdate(rs.getTimestamp("pdate"));
setCont(rs.getString("cont"));
setGrade(0);
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
<3.详细展示帖子内容(将article_detail由静态页面htm转换为动态的JSP页面):
-----article_detail.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page import="java.sql.*,
com.bjsxt.bbs.*" %>
<%
String strId = request.getParameter("id");
//接收Article.jsp传递过来的参数,参数的传递都是以字符串的形式进行的
if(strId == null ||
strId.trim().equals("")) { //判断传递过来的参数是否合法,strId == null ||
strId.trim().equals("")-----这两个检测条件顺序不可调换,因为若参数是null值,则执行strId.trim().equals("")时出现空指针错误
out.println("Error
ID!");
return;
}
int id = 0;
try {
id =
Integer.parseInt(strId);
} catch (NumberFormatException e) {
//若strId为错误的字符串,不可转换为数字时,出现异常
out.println("Error ID
Again!");
return;
}
Article a = null;
Connection conn = DB.getConn();
String sql = "select * from article where
id = " + id;
Statement stmt = DB.createStmt(conn);
ResultSet rs =
DB.executeQuery(stmt, sql);
if(rs.next()) {
a = new
Article();
a.initFromRs(rs);
}
DB.close(rs);
DB.close(stmt);
DB.close(conn);
if(a == null)
{
%>
您寻找的帖子不存在!
<%
return;
}
%>
<!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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="images/global.js"></script>
<link rel="alternate"
type="application/rss+xml" title="RSS"
href="">
</head>
<body>
<table border="0"
cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img
src="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="">首页</a> » <a
href="">ChinaJavaWorld技术论坛|Java世界_中文论坛</a> » <a
href="">Java 2 Platform, Standard Edition (J2SE)</a> » <a
href="">Java语言*初级版</a> </p>
<p
class="jive-page-title"> 主题: <%=a.getTitle() %>
</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=""><img src="images/reply-16x16.gif"
alt="回复本主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-reply-thread"
href="reply.jsp?id=<%=a.getId()%>&rootId=<%=a.getRootId()%>">回复本主题</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="images/level3.gif" title="世界新手"
border="0"><br>
</td>
</tr>
</tbody>
</table>
<img class="jive-avatar"
src="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="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="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="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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="images/global.js"></script>
<!-- fckeditor
-->
<script type="text/javascript"
src="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="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="images/reply-16x16.gif" alt="回复本主题" border="0" height="16"
width="16"></a></td>
<td
class="jive-icon-label"><a id="jive-reply-thread"
href="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="<%=id %>"/>
<input type="hidden" name="rootId" value="<%=rootId %>"/>
//以隐藏域的方式传递pid和rootid给下一个页面
标题:<input type="text"
name="title"><br>
内容:<textarea name="cont" rows="15"
cols="80"></textarea>
<br>
<input type="submit" value="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="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 = url;
//将页面转到url
}
setTimeout("delayURL(‘" + url + "‘)", 1000);
//每秒钟调用递归函数,使delay每一秒减1
}
//-->
</script>
<a href="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="images/style.css"
title="Integrated
Styles">
<script language="JavaScript"
type="text/javascript"
src="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="images/header-stretch.gif"
border="0"
height="57"
width="100%">
</td>
<td
width="1%">
<img src="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="post"
/> //用隐藏域来区分是链接过来还是提交过来的
标题:
<input type="text"
name="title">
<br>
内容:
<textarea name="cont"
rows="15"
cols="80"></textarea>
<br>
<input
type="submit" value="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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="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="images/common.js"></script>
</head>
<body>
<table
border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img
src="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="post.jsp"><img
src="images/post-16x16.gif" alt="发表新主题" border="0" height="16"
width="16"></a></td>
<td
class="jive-icon-label"><a id="jive-post-thread"
href="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="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="articleFlat.jsp?pageNo=<%=pageNo - 1 %>">上一页</a>
</span></span>
<span
class="nobreak"><span class="jive-paginator">|
</span></span>
<span class="nobreak"><span
class="jive-paginator">
<a
href="articleFlat.jsp?pageNo=<%=pageNo + 1 %>">下一页</a>
|
<a href="articleFlat.jsp?pageNo=<%=totalPages
%>">最末页</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";
//判断奇数行偶数行,根据不同的classStr有不同的CSS效果
%>
<tr
class="<%=classStr %>">
<td
class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet">
<img src="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传递给delete.jsp
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="modify.jsp?id=<%=a.getId()%>">MOD</a> //增加更新的链接
<a
href="delete.jsp?id=<%=a.getId()%>&isLeaf=<%=a.isLeaf()%>&pid=<%=a.getPid()
%>&from=<%=url %>">DEL</a>
<%}
%>
</td>
<td
class="jive-thread-name" width="95%"><a id="jive-thread-1"
href="articleDetailFlat.jsp?id=<%=a.getId() %>"><%=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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="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="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="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="images/level3.gif" title="世界新手"
border="0"><br>
</td>
</tr>
</tbody>
</table>
<img class="jive-avatar"
src="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="images/reply-16x16.gif" alt="回复本主题" border="0"
height="16" width="16"></a> </td>
<td class="jive-icon-label"><a
href="reply.jsp?id=<%=a.getId() %>&rootId=<%=a.getRootId() %>"
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="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="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="login"/>
//使用隐藏域区分是自身还是点删除时转到此页面的
用户名:
<input type="text" name="username"
value="<%=username %>"/> //value="<%=username
%>-----小技巧,可以记住之前输入的用户名
<br>
密码:
<input type="password"
name="password" />
<br>
<input type="submit"
value="login" />
</form>
</body>
</html>
<10.加入一个检查session的页面:
----- _SessionCheck.jsp -----
<%
String adminLogined =
(String)session.getAttribute("adminLogined");
if(adminLogined == null ||
!adminLogined.trim().equals("true"))
{
response.sendRedirect("login.jsp");
return;
//当转到另一个页面时加上return,使这个页面不再进行
}
%>
<10.程序打包问题(Web程序):
<<1.在项目上点击export/j2ee/war file/选择存放路径/finish
<<2.将生成的war文件拷贝到web server(tomcat)上,启动web
server,即可浏览文件。
<11.加入管理员更新操作:
-----modify.jsp-----
<%@ page pageEncoding="GB18030"%>
<%@ page
import="java.sql.*,com.bjsxt.bbs.*"%>
<%
request.setCharacterEncoding("GBK");
String action =
request.getParameter("action");
int id =
Integer.parseInt(request.getParameter("id"));
//接收传递过来的id,让数据库知道要修改的帖子的id
if (action != null &&
action.trim().equals("modify")) {
Connection conn =
DB.getConn();
String title = request.getParameter("title");
//获得修改后的title
System.out.println(title);
String cont =
request.getParameter("cont");
//获得修改后的cont
System.out.println(cont);
PreparedStatement pstmt =
DB.prepareStmt(conn, "update article set title = ? , cont = ? where id =
?"); //更新数据库
pstmt.setString(1, title);
pstmt.setString(2,
cont);
pstmt.setInt(3,
id);
pstmt.executeUpdate();
DB.close(pstmt);
DB.close(conn);
response.sendRedirect("articleFlat.jsp");
return;
}
%>
<% //从数据库中取出要更新的那个帖子
Connection conn = DB.getConn();
Statement
stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt, "select *
from article where id = " + id);
if(!rs.next()) return;
//确定rs中是否有内容
Article a = new Article();
a.initFromRs(rs);
//取出其中的内容
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>
<!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="images/style.css"
title="Integrated
Styles">
<script language="JavaScript"
type="text/javascript"
src="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="images/header-stretch.gif"
border="0"
height="57"
width="100%">
</td>
<td
width="1%">
<img src="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%"><br>
<br></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="modify.jsp" method="post">
<input type="hidden" name="action" value="modify" />
//利用隐藏域判断是从哪个页面链接过来的
<input type="hidden" name="id" value="<%=id
%>"/> //将要修改的那个帖子的id传递出去
标题:
<input type="text"
name="title" value="<%=a.getTitle() %>">
//将取出的帖子的标题显示出来
<br>
内容:
<textarea name="cont" rows="15"
cols="80"><%=a.getCont() %></textarea>
//将取出的帖子的内容显示出来
<br>
<input type="submit"
value="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="images/style.css" title="Integrated
Styles">
<script language="JavaScript" type="text/javascript"
src="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="images/common.js"></script>
</head>
<body>
<table
border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="40%"><img
src="images/header-stretch.gif" border="0" height="57"
width="100%">
</td>
<td width="1%"><img
src="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="post.jsp"><img
src="images/post-16x16.gif" alt="发表新主题" border="0" height="16"
width="16"></a></td>
<td
class="jive-icon-label"><a id="jive-post-thread"
href="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="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="searchResult.jsp?pageNo=<%=pageNo - 1 %>">上一页</a>
</span></span>
<span
class="nobreak"><span class="jive-paginator">|
</span></span>
<span class="nobreak"><span
class="jive-paginator">
<a
href="searchResult.jsp?pageNo=<%=pageNo + 1 %>&keyword=<%=keyword
%>">下一页</a>
|
<a
href="articleFlat.jsp?pageNo=<%=totalPages %>">最末页</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="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="modify.jsp?id=<%=a.getId()%>">MOD</a>
<a
href="delete.jsp?id=<%=a.getId()%>&isLeaf=<%=a.isLeaf()%>&pid=<%=a.getPid()
%>&from=<%=url %>">DEL</a>
<%}
%>
</td>
<td
class="jive-thread-name" width="95%"><a id="jive-thread-1"
href="articleDetailFlat.jsp?id=<%=a.getId() %>"><%=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>