用 Java 實現評論功能,這節是實現評論,之後會補充回覆功能,下面是效果圖(此處不會詳解頁面的設計,哈哈哈,因爲頁面也不是我設計的,而且這個頁面還被我玩壞了)
其實和我上一篇博客的知識很是相近
1. 創建數據庫 (使用 MySQL 數據庫)
2. 創建 Comment 類(評論)、Reply 類(回覆)
3. 連接數據庫
4. 將相應的添加評論進數據庫
5. 添加回復進數據庫
6. 從數據庫中取出元素並創建 Comment 或 Reply 類,並將其放進 commentList 或 replyList 中
7. 在 jsp 頁面中循環數組,將需要的數據進行展示即可
下面分開來看代碼:
1. 創建數據庫 (使用 MySQL 數據庫)
Comment 數據庫下的兩個表
1. Comment 表
create table Comment(
comment_id serial NOT NULL, //評論id號
user_id varchar(11), //用戶id號
content varchar(50), //評論內容
createtime varchar(25) //創建評論的時間
);
2. Reply 表
create table Reply(
comment_id serial NOT NULL, //評論id號
user_id varchar(11), //用戶id號
replyuser_id varchar(11), //被回覆用戶的id號
content varchar(50), //評論內容
createtime varchar(25) //創建評論的時間
);
2. 創建 Comment 類(評論)、Reply 類(回覆)
記得將這兩個類創建成 javaBeans 的形式,即每一個變量都是 private ,而且都有相應的 get 和 set 的方法。
Comment 類有以下變量
private int comment_id; //評論id號 int
private String user_id; //用戶id varchar(11)
private String username; //用戶名 varchar(10)
private String content; //評論內容 varcar(50)
private String createtime; //評論時間 varchar(25)
Reply 類有以下變量
private int comment_id; //評論id int
private String user_id; //用戶id varchar(11)
private String username; //用戶名 varchar(10)
private String replyuser_id; // 被回覆人id varchar(11)
private String content; //回覆內容 varchar(50)
private String createtime; //回覆時間 varchar(25)
3. 連接數據庫
public class BaseDao {
public Connection getConnection() {
// 相關參數的設置
String username = "root";// 數據庫用戶名
String password = "**********";// 數據庫密碼
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/comment";
// 數據庫的連接
try {
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
4. 將相應的添加評論進數據庫 ( 4,5,6 點都在 CommentServlet 的方法中實現)
先在 init() 方法中連接數據庫
Connection dbconn = null;
public void init() throws ServletException {
dbconn = new BaseDao().getConnection();
}
public void addComment(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession();
String user_id = (String) session.getAttribute("user_id");// 得到該登錄用戶的id號
String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8");
String createtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String sql = "insert into Comment(user_id,content,createtime) value(?,?,?)";
try {// 將評論添加進數據庫
PreparedStatement pstmt = dbconn.prepareStatement(sql);
pstmt.setString(1, user_id);
pstmt.setString(2, content);
pstmt.setString(3, createtime);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ListComment display = new ListComment(dbconn);//將數據庫中的評論及回覆信息加載出來並進行顯示
display.listComment(request, response);
}
一般帶有評論功能的網站都是需要登錄的,所以先把該用戶的 user_id 放在 session 中,然後再取出即可。下面同樣
5. 添加回復進數據庫
public void addReply(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
try {
String replyuser_id = request.getParameter("replyuser_id");// 得到被評論用戶的id號
HttpSession session = request.getSession();// 得到該登錄用戶的id號
String user_id = (String) session.getAttribute("user_id");
String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8");// 評論
String createtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());// 時間
String sql = "insert into reply(replyuser_id,user_id,content,createtime) value(?,?,?,?)";
// 將回復添加進comment數據庫 中的 reply表
try {
PreparedStatement pstmt = dbconn.prepareStatement(sql);
pstmt.setString(1, replyuser_id);
pstmt.setString(2, user_id);
pstmt.setString(3, content);
pstmt.setString(4, createtime);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ListComment display = new ListComment(dbconn);//將數據庫中的評論及回覆信息加載出來並進行顯示
display.listComment(request, response);
}
6. 從數據庫中取出元素並創建 Comment 或 Reply 類,並將其放進 commentList 或 replyList 中
public void listComment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
List<Comment> commentList = new ArrayList<Comment>();// 評論數組
List<Reply> replyList = new ArrayList<Reply>();// 回覆數組
String commentSql = "select * from Comment";
String replySql = "select * from Reply";
PreparedStatement pstmt;
try { // 添加評論
pstmt = dbconn.prepareStatement(commentSql);
ResultSet rst = pstmt.executeQuery();
while (rst.next()) {
int comment_id = rst.getInt("comment_id"); //得到數據庫中的數據
String user_id = rst.getString("user_id");
//得到用戶名,需要在 Comment 數據庫中有一個User 表,該表中至少 含有 user_id 和 username 兩個屬性(下同)
String username = getUserName(user_id);
String content = rst.getString("content");
String createtime = rst.getString("createtime");
Comment comment = new Comment();//創建 Comment 類
comment.setComment_id(comment_id);
comment.setUser_id(user_id);
comment.setUsername(username);
comment.setContent(content);
comment.setCreatetime(createtime);
commentList.add(comment);// 將此評論添加到數組中
}
} catch (SQLException e) {
e.printStackTrace();
}
try { // 添加回復
pstmt = dbconn.prepareStatement(replySql);
ResultSet rst = pstmt.executeQuery();
while (rst.next()) {
int comment_id = rst.getInt("comment_id"); //得到數據庫中相應數據
String user_id = rst.getString("user_id");
String replyuser_id = rst.getString("replyuser_id");
String username = getUserName(user_id);
String content = rst.getString("content");
String createtime = rst.getString("createtime");
Reply reply = new Reply(); //創建 reply 類
reply.setComment_id(comment_id);
reply.setUser_id(user_id);
reply.setUsername(username);
reply.setReplyuser_id(replyuser_id);
reply.setContent(content);
reply.setCreatetime(createtime);
replyList.add(reply);// 將此評論添加到數組中
}
} catch (SQLException e) {
e.printStackTrace();
}
HttpSession session = request.getSession(); //得到 session
session.setAttribute("commentList", commentList); //將 評論數組 和 回覆數據 均放入 session 中
session.setAttribute("replyList", replyList);
RequestDispatcher rd = request.getRequestDispatcher("HavaAComment.jsp");//跳轉至評論顯示頁面
rd.forward(request, response);
}
得到 username 的方法,得到用戶名,需要在 Comment 數據庫中有一個User 表,該表中至少 含有 user_id 和 username 兩個屬性
public String getUserName(String user_id) {
String sql = "select username from user where user_id=?";
String username = null;
try {
PreparedStatement pstmt = dbconn.prepareStatement(sql);
pstmt.setString(1, user_id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
username = rs.getString("username");
}
} catch (SQLException e) {
e.printStackTrace();
}
return username;
}
7. 在 jsp 頁面中循環數組,將需要的數據進行展示即可(就是因爲這裏還沒有完成,所以導致回覆功能還沒有完成)
下面的 HaveAComment.jsp 可能有些細節問題,因爲這是我從中截取出來的,但是核心內容已經寫在了下面。
<html>
<body>
<!-- 評論展示-->
<c:forEach var="comment" items="${commentList }">
<div>
<h4><a href="#">${comment.username }</a></h4>
<span><a href="#">${comment.createtime }</a></span>
<p>${comment.content }</p>
<p><span class="reply"><a href="#">Reply</a></span></p>
</div>
</c:forEach>
<!-- 評論書寫框 -->
<div>
<h3>Leave a comment</h3>
<form action="addComment.do?action=addComment" method="post">
<fieldset>
<br style="clear:both" />
<textarea cols="25" rows="5" name="content" >Message</textarea>
<br style="clear:both" />
<input type="submit" name="submit" class="button" value="Submit"/>
</fieldset>
</form>
</div>
</body>
</html>
上面就是評論功能的大概實現,還沒有回覆,改日定會回來加上。