简单BBS的的回顾:
开始前需注意的是必须引入:相应数据库的JDBC的驱动文件,引入到webcont的WEB-INF下的lib文件下,然后建库!嘿嘿,需要注意!
以下是代码:
ShowArticleTree.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
   
<%@ page import="java.sql.*" %>
<%
String admin = (String)session.getAttribute("admin");
System.out.println(admin);
if(admin!=null && admin.equals("true")) {
 login = true;
}
%>
<%!
boolean login = false;
String str = "";
public void tree(Connection conn,int id,int leve) {
 Statement stmt = null;
 ResultSet rs = null;
 String pleve = "";
 for (int i=0;i<leve;i++) {
  pleve +="----";
 }
 try {
  stmt = conn.createStatement();
  String sql = "select * from article where pid = " + id;
  rs = stmt.executeQuery(sql);
  String strLogin = "";
  while(rs.next()) {
   int idd = rs.getInt("id");
   int pidd = rs.getInt("pid");
   if(login) {
    strLogin = "<a href = 'Delete.jsp?id=" + idd + "&pid=" + pidd + "'>删除</a>";
   }
   str += "<tr><td>" + idd + "</td><td>" +
         pleve + "<a href = 'ArticleShowDetail.jsp?id=" + idd + "'>"+
         rs.getString("title") + "</a></td><td>" +
         strLogin+
         "</td></tr>";
   
   if(rs.getInt("isleaf") != 0) {
    tree(conn,idd,leve+1);
   }
  }
 } catch(SQLException e) {
  e.printStackTrace();
 } finally {
  try {
   if(rs != null) {
    rs.close();
    rs = null;
   }
   if(stmt != null) {
    stmt.close();
    stmt = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}
%>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English";
 String use = "sa";
 String password = "123456";
 conn = DriverManager.getConnection(url,use,password);
 
 stmt = conn.createStatement();
 rs = stmt.executeQuery("select * from article where pid = 0");
 String strLogin = "";
 while(rs.next()) {
  int id = rs.getInt("id");
  int pid = rs.getInt("pid");
  if(login) {
   strLogin = "<a href = 'Delete.jsp?id=" + id + "&pid=" + pid + "'> 删除 </a>";
  }
  str += "<tr><td>" + id + "</td><td>" +
         "<a href = 'ArticleShowDetail.jsp?id=" + id + "'>"+
         rs.getString("title") + "</a></td><td>" +
         strLogin +
         "</td></tr>";      
     if(rs.getInt("isleaf") != 0) {
      tree(conn,id,1);
     }
 }
} catch (ClassNotFoundException e) {
 e.printStackTrace();
} catch (SQLException e) {
 e.printStackTrace();
} finally {
 try {
  if(rs != null) {
   rs.close();
   rs = null;
  }
  if(stmt != null) {
   stmt.close();
   stmt = null;
  }
  if(conn != null) {
   conn.close();
   conn = null;
  }
 } catch (SQLException e) {
  e.printStackTrace();
 }
}
%>
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<a href="Post.jsp">发表新帖</a>
<table border = "1">
<%=str%>
<%
str = "";
login = false;
%>
</table>
</body>
</html>
 
ArticleShowDetail.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English";
 String use = "sa";
 String password = "123456";
 conn = DriverManager.getConnection(url,use,password);
 
 stmt = conn.createStatement();
 String sql = "select * from article where id = " + id;
 rs = stmt.executeQuery(sql);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<table border = "1">
<%
if(rs.next()) {
 int rootid = rs.getInt("rootid");
%>
 <tr>
  <td>ID</td>
  <td><%=id%></td>
 </tr>
 <tr>
  <td>Title</td>
  <td><%=rs.getString("title") %></td>
 </tr>
 <tr>
  <td>Content</td>
  <td><%=rs.getString("cont") %></td>
 </tr>
</table>
  <a href = "Reply.jsp?pid=<%=id%>&&rootid=<%=rootid%>">回复</a>
  <%
}
%>
</body>
<%
} catch (ClassNotFoundException e) {
 e.printStackTrace();
} catch (SQLException e) {
 e.printStackTrace();
} finally {
 try {
  if(rs != null) {
   rs.close();
   rs = null;
  }
  if(stmt != null) {
   stmt.close();
   stmt = null;
  }
  if(conn != null) {
   conn.close();
   conn = null;
  }
 } catch (SQLException e) {
  e.printStackTrace();
 }
}
%>
</html>
 
Reply.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%
int pid = Integer.parseInt(request.getParameter("pid"));
int rootid = Integer.parseInt(request.getParameter("rootid"));
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<script language="javascript">
<!--
 function LTrim(str) {
  var i;
  for(i=0;i<str.length;i++) {
   if(str.charAt(i)!=" ") break;
  }
  str = str.substring(i,str.length);
  return str;
 }
 function RTrim(str) {
  var i;
  for(i=str.length-1;i>=0;i--) {
   if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
  }
  str = str.substring(0,i+1);
  return str;
 }
 function Trim(str) {
  return LTrim(RTrim(str));
 }
 function check() {
  if(Trim(document.reply.title.value)=="") {
   alert("请输入标题!");
   document.reply.title.focus();
   return false;
  }
  if(Trim(document.reply.cont.value)=="") {
   alert("请输入内容!");
   document.reply.cont.focus();
   return false;
  }
  return true;
 }
-->
</script>
</head>
<body>
<form name=reply action="ReplyOk.jsp" method="post" onsubmit="return check()">
 <input type="hidden" name="pid" value="<%=pid %>">
 <input type="hidden" name="rootid" value="<%=rootid %>">
 <table border="1">
  <tr>
   <td>
    <input type="text" size="80" name="title">
   </td>
  </tr>
  <tr>
   <td>
    <textarea cols="80" rows="12" name="cont"></textarea>
   </td>
  </tr>
  <tr>
   <td>
    <input type="submit" value="提交" name="submit">
   </td>
  </tr>
 </table>
</form>
</body>
</html>
 
ReplyOk.jsp
 
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("gbk");
int pid = Integer.parseInt(request.getParameter("pid"));
int rootid = Integer.parseInt(request.getParameter("rootid"));
String title = request.getParameter("title");
if(title == null) {
 out.print("请用正确的方式访问本人的BBS!!注意点!");
 return;
}
title = title.trim();
if(title.equals("")) {
 out.print("标题不能为空!");
 return;
}
String cont = request.getParameter("cont");
cont = cont.trim();
if(cont.equals("")) {
 out.print("内容不能为空!");
 return;
}
cont = cont.replaceAll("\n","<br>");
%>
<%
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English;SelectMethod=cursor";
 String use = "sa";
 String password = "123456";
 Connection conn = DriverManager.getConnection(url,use,password);
 
 conn.setAutoCommit(false);
 
 String sql = "insert into article values(?,?,?,?,getdate(),0)";
 PreparedStatement pstmt = conn.prepareStatement(sql);
 Statement stmt = conn.createStatement();
 pstmt.setInt(1,pid);
 pstmt.setInt(2,rootid);
 pstmt.setString(3,title);
 pstmt.setString(4,cont);
 pstmt.executeUpdate();
 
 stmt.executeUpdate("update article set isleaf = 1 where id = " + pid);
 
 conn.commit();
 conn.setAutoCommit(true);
 
 stmt.close();
 pstmt.close();
 conn.close();
response.sendRedirect("ShowArticleTree.jsp");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
 
Delete.jsp
 
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%
int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid"));
%>
<%!
public void delete(Connection conn,int id) {
 Statement stmt = null;
 ResultSet rs = null;
 
 try {
  stmt = conn.createStatement();
  String sql = "select * from article where pid = " + id;
  rs = stmt.executeQuery(sql);
  
  while(rs.next()) {
   delete(conn,rs.getInt("id"));
  }
  stmt.executeUpdate("delete from article where id="+id);
  
 } catch(SQLException e) {
  e.printStackTrace();
 } finally {
  try {
   if(rs != null) {
    rs.close();
    rs = null;
   }
   if(stmt != null) {
    stmt.close();
    stmt = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}
%>
<%
String admin = (String)session.getAttribute("admin");
if(admin == null||!admin.equals("true")) {
 out.print("请先登录!!!");
 return;
}
%>
<%
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English;SelectMethod=cursor";
 String use = "sa";
 String password = "123456";
 Connection conn = DriverManager.getConnection(url,use,password);
 conn.setAutoCommit(false);
 
 delete(conn,id);
 
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("select count(*) from article where pid=" + pid);
 rs.next();
 int count = rs.getInt(1);
 if(count <= 0) {
  Statement stmtUpdate = conn.createStatement();
  stmtUpdate.executeUpdate("update article set isleaf = 0 where id=" + pid);
  stmtUpdate.close();
 }
 conn.commit();
 conn.setAutoCommit(true);
 rs.close();
 stmt.close();
 conn.close();
response.sendRedirect("ShowArticleTree.jsp");
%>
 
Post.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("gbk");
String action = request.getParameter("action");
if(action!=null && action.equals("post")) {
 
 String title = request.getParameter("title");
 
 if(title == null) {
  out.print("请用正确的方式访问本人的BBS!!注意点!");
  return;
 }
 title = title.trim();
 if(title.equals("")) {
  out.print("标题不能为空!");
  return;
 }
 
 String cont = request.getParameter("cont");
 
 cont = cont.trim();
 if(cont.equals("")) {
  out.print("内容不能为空!");
  return;
 }
 
 cont = cont.replaceAll("\n","<br>");
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English;SelectMethod=cursor";
 String use = "sa";
 String password = "123456";
 Connection conn = DriverManager.getConnection(url,use,password);
 
 conn.setAutoCommit(false);
 
 String sql = "insert into article values(0,-1,?,?,getdate(),0)";
 PreparedStatement pstmt = conn.prepareStatement(sql);
 Statement stmt = conn.createStatement();
 //pstmt.setInt(1,-1);
 pstmt.setString(1,title);
 pstmt.setString(2,cont);
 pstmt.executeUpdate();
 
 stmt.executeUpdate("update article set rootid = id where rootid = -1 " );
 
 conn.commit();
 conn.setAutoCommit(true);
 
 stmt.close();
 pstmt.close();
 conn.close();
response.sendRedirect("ShowArticleTree.jsp");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<script language="javascript">
<!--
 function LTrim(str) {
  var i;
  for(i=0;i<str.length;i++) {
   if(str.charAt(i)!=" ") break;
  }
  str = str.substring(i,str.length);
  return str;
 }
 function RTrim(str) {
  var i;
  for(i=str.length-1;i>=0;i--) {
   if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
  }
  str = str.substring(0,i+1);
  return str;
 }
 function Trim(str) {
  return LTrim(RTrim(str));
 }
 function check() {
  if(Trim(document.post.title.value)=="") {
   alert("请输入标题!");
   document.post.title.focus();
   return false;
  }
  if(Trim(document.post.cont.value)=="") {
   alert("请输入内容!");
   document.post.cont.focus();
   return false;
  }
  return true;
 }
-->
</script>
</head>
<body>
<form action="Post.jsp" method="post" name="post" onsubmit="return check()">
 <input type="hidden" name="action" value="post">
 <table border="1">
  <tr>
   <td>
    <input type="text" size="80" name="title">
   </td>
  </tr>
  <tr>
   <td>
    <textarea cols="80" rows="12" name="cont"></textarea>
   </td>
  </tr>
  <tr>
   <td>
    <input type="submit" value="提交">
   </td>
  </tr>
 </table>
</form>
</body>
</html>
 
Login.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%
String action = request.getParameter("action");
if(action != null&&action.equals("post")) {
 String user = request.getParameter("user");
 String password = request.getParameter("password");
 if(user==null||!user.equals("admin")) {
  out.print("用户名输入的不正确!请重新输入!");
  //return;
 }else if(password==null||!password.equals("admin")) {
  out.print("密码输入不正确!请从新输入!");
  //return;
 }else {
  session.setAttribute("admin","true");
  response.sendRedirect("ShowArticleTree.jsp");
 }
}
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<form action="Login.jsp" method="post">
 <input type="hidden" name="action" value="post">
 <table border="1">
  <tr>
   <td>
    用户名:
   </td>
   <td>
    <input type="text" size="20" name="user">
   </td>
  </tr>
  <tr>
   <td>
    密码:
   </td>
   <td>
    <input type="password" size="20" name="password">
   </td>
  </tr>
  <tr>
   <td>
    <input type="submit" value="提交">
   </td>
  </tr>
 </table>
</form>
</body>
</html>
 
ShowArticleFlat.jsp
 
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
   
<%@ page import="java.sql.*" %>
<%
int pageSize = 3;
String strPageNo = request.getParameter("pageNo");
int pageNo;
if(strPageNo == null || strPageNo.equals("")) {
 pageNo = 1; 
}else {
 try {
  pageNo = Integer.parseInt(strPageNo.trim());
 } catch(NumberFormatException e) {
  pageNo = 1;
 }
 if(pageNo<=0) {
  pageNo = 1;
 }
}
%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=English;SelectMethod=cursor";
String use = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url,use,password);
Statement stmtCount = conn.createStatement();
ResultSet rsCount = stmtCount.executeQuery("select count(*) from article where pid = 0");
rsCount.next();
int countRecords = rsCount.getInt(1);
int countPages = countRecords % pageSize ==0?countRecords/pageSize : countRecords/pageSize+1;
if(pageNo>countPages) {
 pageNo = countPages;
}
int last = pageSize*(pageNo-1);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select top "+pageSize+" * from article where pid = 0 and (id not in (select top "+last+" id from article order by id desc)) order by id desc");

%>
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<a href="Post.jsp">发表新帖</a>
<table border = "1">
 <%
 while(rs.next()) {
  int id = rs.getInt("id");
  String title = rs.getString("title");
 %>
  <tr>
   <td><%=id %></td>
   <td>
    <a href="ArticleShowDetail.jsp?id=<%=id%>"> <%=title %> </a>
   </td>
  </tr>
 <%
 }
 %>
</table>
共<%=countPages %>页&nbsp;&nbsp;第<%=pageNo %>页&nbsp;&nbsp;
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo-1 %>">上一页</a>&nbsp;&nbsp;
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo+1 %>">下一页</a>
<form name="form1" action="ShowArticleFlat.jsp">
 <select name="pageNo" onchange="document.form1.submit()">
  <%
   for(int i=1;i<=countPages ;i++) {
  %>
    <option value=<%=i %> <%=pageNo==i?"selected":"" %>>第<%=i %>页
  <%
   }
  %>
 </select>
</form>
<form action="ShowArticleFlat.jsp" name="form2">
 <input type="text" size="4" name="pageNo" value="<%=pageNo %>">
 <input type="submit" value="go">
</form>

</body>
<%
rs.close();
stmt.close();
conn.close();
%>
</html>
 
 
这里面有许多细节性的东西!需要注意!(针对于我)在这里就不写注释了,幸福的郁闷!