首页 > 代码库 > 简单BBS项目
简单BBS项目
项目的基本要求:
掌握递归
掌握JavaScript+html+css+jsp+servlet初步应用
掌握分页写法
初步掌握前台/后台的概念
简单的功能需求:
1.能够树形展现
2.能够平板型展现
3.能够回复帖子
4.后台能够管理帖子,如:删除
<1.新建项目:右键new/project/Dynamic Web Project,项目名称为BBS,配置tomcat。
<2.设计数据库的表:保留建表的语句到项目中,在项目new/folder然后引入bbs.sql;
*****bbs.sql*****
create database bbs;
use bbs;
create table article
(
id int primary key auto_increment, //auto_increment--为mysql中的自动递增
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int
);
insert into article values (null, 0, 1, ‘蚂蚁大战大象‘, ‘蚂蚁大战大象‘, now(), 1); //因为第一个字段自动递增,所以为null值
insert into article values (null, 1, 1, ‘大象被打趴下了‘, ‘大象被打趴下了‘,now(), 1);
insert into article values (null, 2, 1, ‘蚂蚁也不好过‘,‘蚂蚁也不好过‘, now(), 0);
insert into article values (null, 2, 1, ‘瞎说‘, ‘瞎说‘, now(), 1);
insert into article values (null, 4, 1, ‘没有瞎说‘, ‘没有瞎说‘, now(), 0);
insert into article values (null, 1, 1, ‘怎么可能‘, ‘怎么可能‘, now(), 1);
insert into article values (null, 6, 1, ‘怎么没有可能‘, ‘怎么没有可能‘, now(), 0);
insert into article values (null, 6, 1, ‘可能性是很大的‘, ‘可能性是很大的‘, now(), 0);
insert into article values (null, 2, 1, ‘大象进医院了‘, ‘大象进医院了‘, now(), 1);
insert into article values (null, 9, 1, ‘护士是蚂蚁‘, ‘护士是蚂蚁‘, now(), 0);
批量输入以上语句:进入mysql后台,执行命令:source */bbs.sql //*代表相应的目录
mysql的账号/密码
还要在项目的WebContentde/WEB-INF/LIB中引入mysql-connector-java-5.1.29-bin.jar包
<3.在eclipse中直接查看数据库
在Data Source Explorer中右键点击DataBase/new-->选择Generic JDBC Connection-->随便输入名称然后next-->选择相应的数据库......
<4.新建JSP文件ShowArticleTree.jsp
-----ShowArticleTree.jsp-----
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="java.sql.*"%>
<%
String admin=(String)session.getAttribute("admin"); //在Login.jsp中session.setAttribute("admin","true");-----由此判断是否在登录页面Login.jsp中成功登录
if(admin!=null&&admin.equals("true")){
login=true; //登录成功则讲login设置为true
}
%>
<%!
String str="";
boolean login=false; //设置变量login,判断是否登录
private void tree(Connection conn,int id,int level){
Statement stmt=null;
ResultSet rs=null;
String preStr="";
for(int i=0;i<level;i++){
preStr+="----";
}
try{
stmt=conn.createStatement();
String sql="select * from article where pid ="+id;
String strLogin="";
rs=stmt.executeQuery(sql);
while(rs.next()){
if(login){ //判断是否成功登录
strLogin="<td><a href=http://www.mamicode.com/‘Delete.jsp?id="+rs.getInt("id")+"&pid="+rs.getInt("pid")+"‘>delete</a>"; //设置删除链接
}
str+="<tr><td>"+rs.getInt("id")+"</td><td>"+
"<a href=http://www.mamicode.com/‘ShowArticleDetail.jsp?id="+rs.getInt("id")+"‘>"+rs.getString("title")+"</a>"+strLogin+"</td></tr>"; //若成功登录,则加上删除链接
if(rs.getInt("isleaf")!=0){
tree(conn,rs.getInt("id"),level+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();
}
}
}
%>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/bbs?user=root&password=abc123";
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from article where pid=0");
String strLogin="";
while(rs.next()){
if(login){
strLogin="<td><a href=http://www.mamicode.com/‘Delete.jsp?id="+rs.getInt("id")+"&pid="+rs.getInt("pid")+"‘>delete</a>";
}
str+="<tr><td>"+rs.getInt("id")+"</td><td>"+"<a href=http://www.mamicode.com/‘ShowArticleDetail.jsp?id="+rs.getInt("id")+"‘>"+rs.getString("title")+"</a>"+"</a></td>"+strLogin+"</td></tr>";
if(rs.getInt("isleaf")!=0){
tree(conn,rs.getInt("id"),1);
}
}
rs.close();
stmt.close();
conn.close();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%=str %>
</table>
</body>
</html>
<5.用递归的方法输出树:
-----ShowArticleTree.jsp-----
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="java.sql.*"%>
<%! //定义地柜函数来显示树状的帖子
String str="";
private void tree(Connection conn,int id,int level){
Statement stmt=null;
ResultSet rs=null;
String preStr="";
for(int i=0;i<level;i++){
preStr+="----";
}
try{
stmt=conn.createStatement();
String sql="select * from article where pid ="+id;
rs=stmt.executeQuery(sql);
while(rs.next()){
str+="<tr><td>"+rs.getInt("id")+"</td><td>"+"<a href=http://www.mamicode.com/‘ShowArticleDetail.jsp?id="+rs.getInt("id")+"‘>"+rs.getString("title")+"</a>"+"</a></td>"+"<td><a href=http://www.mamicode.com/‘Delete.jsp?id="+rs.getInt("id")+"&pid="+rs.getInt("pid")+"‘>删除</a>"+"</td></tr>";
if(rs.getInt("isleaf")!=0){
tree(conn,rs.getInt("id"),level+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();
}
}
}
%>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/bbs?user=root&password=abc123";
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from article where pid=0");
while(rs.next()){
str+="<tr><td>"+rs.getInt("id")+"</td><td>"+"<a href=http://www.mamicode.com/‘ShowArticleDetail.jsp?id="+rs.getInt("id")+"‘>"+rs.getString("title")+"</a>"+"</a></td>"+"<td><a href=http://www.mamicode.com/‘Delete.jsp?id="+rs.getInt("id")+"&pid="+rs.getInt("pid")+"‘>删除</a>"+"</td></tr>";
if(rs.getInt("isleaf")!=0){
tree(conn,rs.getInt("id"),1);
}
}
rs.close();
stmt.close();
conn.close();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%=str %>
<%str=""; %> //将字符串置为空,避免刷新时重复显示
</table>
</body>
</html>
<6.创建jsp文件显示帖子的具体内容(从数据库中取内容)
-----ShowArticleDetil.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" "http://www.w3.org/TR/html4/loose.dtd">
<%
String strid=request.getParameter("id"); //获得ShowArticleTree.jsp的表格链接过来的id
int id=Integer.parseInt(strid); //将获得字符串转为Int
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/bbs?user=root&password=abc123";
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from article where id="+id);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<%
if(rs.next()){
%>
<table border="1">
<tr>
<td>Id</td>
<td><%=rs.getInt("id") %></td>
</tr>
<tr>
<td>Title</td>
<td><%=rs.getString("id") %></td>
</tr>
<tr>
<td>Context</td>
<td><%=rs.getString("cont") %></td>
</tr>
</table>
<a href="http://www.mamicode.com/Reply.jsp?id="> //以隐藏域的方式传递参数
<input type="hidden" name="rootid" value="http://www.mamicode.com/ /> //文本框中的值初始为pageNo的值
<input type="submit" value="http://www.mamicode.com/go" />
</form>
</body>
</html>