jsp评论区功能代码实现
时间: 2023-07-30 21:04:24 浏览: 194
以下是一个简单的 JSP 评论区功能代码实现,包括评论输入框、提交按钮、评论列表和回复功能:
1. 创建数据库表,表名为 comment,包括以下字段:
```sql
CREATE TABLE comment (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
2. 在 JSP 页面中添加评论输入框和提交按钮:
```html
<form action="comment.jsp" method="post">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required><br>
<label for="content">评论:</label>
<textarea id="content" name="content" required></textarea><br>
<button type="submit">提交</button>
</form>
```
3. 在 comment.jsp 中处理表单提交并将评论信息存储到数据库中:
```jsp
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name");
String content = request.getParameter("content");
if (name != null && content != null) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/comment?useSSL=false", "root", "password");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO comment (name, content) VALUES (?, ?)");
stmt.setString(1, name);
stmt.setString(2, content);
stmt.executeUpdate();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
response.sendRedirect("comment.jsp");
%>
```
4. 在 comment.jsp 中读取数据库中的评论信息并显示在页面上:
```jsp
<%@ page import="java.sql.*" %>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/comment?useSSL=false", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM comment ORDER BY time DESC");
while (rs.next()) {
String name = rs.getString("name");
String content = rs.getString("content");
String time = rs.getString("time");
%>
<div class="comment">
<div class="comment-name"><%=name%></div>
<div class="comment-time"><%=time%></div>
<div class="comment-content"><%=content%></div>
<div class="comment-reply">
<a href="#" onclick="reply('<%=name%>');">回复</a>
</div>
</div>
<%
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
```
5. 在 comment.jsp 中添加回复功能,即在评论下方添加回复输入框和提交按钮:
```html
<div id="reply-form" style="display:none;">
<form action="comment.jsp" method="post">
<input type="hidden" id="reply-to" name="reply-to">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required><br>
<label for="content">回复:</label>
<textarea id="content" name="content" required></textarea><br>
<button type="submit">提交</button>
</form>
</div>
<script>
function reply(name) {
document.getElementById("reply-to").value = name;
document.getElementById("reply-form").style.display = "block";
}
</script>
```
6. 在 comment.jsp 中处理回复表单提交并将回复信息存储到数据库中,回复信息需要包含被回复者的姓名和评论 ID:
```jsp
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name");
String content = request.getParameter("content");
String replyTo = request.getParameter("reply-to");
String id = request.getParameter("id");
if (name != null && content != null) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/comment?useSSL=false", "root", "password");
PreparedStatement stmt;
if (replyTo == null) {
stmt = conn.prepareStatement("INSERT INTO comment (name, content) VALUES (?, ?)");
stmt.setString(1, name);
stmt.setString(2, content);
} else {
stmt = conn.prepareStatement("INSERT INTO comment (name, content, reply_to) VALUES (?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, content);
stmt.setInt(3, Integer.parseInt(id));
}
stmt.executeUpdate();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
response.sendRedirect("comment.jsp");
%>
```
这是一个简单的 JSP 评论区功能实现,可以根据实际需求进行修改和扩展。需要注意的是,这里使用了 JDBC 连接数据库,需要导入相应的 JDBC 驱动包。
阅读全文