JSP(JavaServer Pages)是一种用于创建动态Web内容的技术,它允许开发者将Java代码嵌入到HTML页面中,从而实现动态内容的生成。下面是一个简单的JSP实现评论与回复功能的源码示例:
首先,我们需要设计一个简单的数据库表来存储评论和回复信息。假设我们有一个名为comments
的表,结构如下:
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT NOT NULL,
user_id INT NOT NULL,
parent_id INT DEFAULT NULL, -- NULL表示这是顶级评论,非NULL表示这是回复
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
创建一个JSP页面来显示评论和回复。
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>评论与回复</title>
</head>
<body>
<h1>评论与回复</h1>
<form action="submit_comment.jsp" method="post">
<textarea name="content" required></textarea><br>
<input type="hidden" name="parent_id" value="0">
<input type="submit" value="提交评论">
</form>
<%
// 连接数据库
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
// 查询所有评论
pstmt = conn.prepareStatement("SELECT * FROM comments WHERE parent_id IS NULL ORDER BY created_at DESC");
rs = pstmt.executeQuery();
while (rs.next()) {
int commentId = rs.getInt("id");
String content = rs.getString("content");
int userId = rs.getInt("user_id");
Timestamp createdAt = rs.getTimestamp("created_at");
%>
<div>
<p><%= content %></p>
<p>用户ID: <%= userId %>, 时间: <%= createdAt %></p>
<a href="reply.jsp?id=<%= commentId %>">回复</a>
<!-- 查询该评论的所有回复 -->
<%
pstmt = conn.prepareStatement("SELECT * FROM comments WHERE parent_id = ?");
pstmt.setInt(1, commentId);
rs = pstmt.executeQuery();
while (rs.next()) {
String replyContent = rs.getString("content");
int replyUserId = rs.getInt("user_id");
Timestamp replyCreatedAt = rs.getTimestamp("created_at");
%>
<div style="margin-left: 20px;">
<p><%= replyContent %></p>
<p>用户ID: <%= replyUserId %>, 时间: <%= replyCreatedAt %></p>
</div>
<%
}
%>
</div>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
</body>
</html>
创建一个JSP页面来处理评论的提交。
<%@ page import="java.sql.*" %>
<%
String content = request.getParameter("content");
int parentId = Integer.parseInt(request.getParameter("parent_id"));
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
pstmt = conn.prepareStatement("INSERT INTO comments (content, user_id, parent_id) VALUES (?, ?, ?)");
pstmt.setString(1, content);
pstmt.setInt(2, 1); // 假设当前用户ID为1
pstmt.setInt(3, parentId);
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
创建一个JSP页面来处理回复的提交。
<%@ page import="java.sql.*" %>
<%
int commentId = Integer.parseInt(request.getParameter("id"));
String content = request.getParameter("content");
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
pstmt = conn.prepareStatement("INSERT INTO comments (content, user_id, parent_id) VALUES (?, ?, ?)");
pstmt.setString(1, content);
pstmt.setInt(2, 1); // 假设当前用户ID为1
pstmt.setInt(3, commentId);
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
通过以上步骤和示例代码,你可以实现一个基本的评论与回复功能。如果有更多具体问题或需要进一步的优化,欢迎继续提问。
领取专属 10元无门槛券
手把手带您无忧上云