答案:通过Servlet、JSP和MySQL实现博客评论系统,包含数据库设计、实体类、DAO层、两个Servlet处理展示与提交、JSP页面显示及表单输入,完成评论的增删查功能。

开发一个简易博客评论系统在Java中可以通过结合Servlet、JSP和数据库来实现。整个系统不需要复杂框架,适合初学者理解Web应用的基本流程。核心功能包括显示评论、提交评论和数据持久化。
使用MySQL存储评论数据,创建一张简单的评论表:
CREATE TABLE comment (
id INT AUTO_INCREMENT PRIMARY KEY,
author VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
该表包含评论ID、作者名、评论内容和发布时间。
定义Comment类映射数据库记录:
立即学习“Java免费学习笔记(深入)”;
public class Comment {
private int id;
private String author;
private String content;
private Timestamp createTime;
<pre class='brush:java;toolbar:false;'>// 构造方法、getter和setter省略}
编写CommentDao类用于数据库操作:
public class CommentDao {
private String jdbcURL = "jdbc:mysql://localhost:3306/blog";
private String jdbcUsername = "root";
private String jdbcPassword = "password";
private Connection getConnection() { /* 获取连接 */ }
<pre class='brush:java;toolbar:false;'>public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<>();
String sql = "SELECT * FROM comment ORDER BY create_time DESC";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Comment c = new Comment();
c.setId(rs.getInt("id"));
c.setAuthor(rs.getString("author"));
c.setContent(rs.getString("content"));
c.setCreateTime(rs.getTimestamp("create_time"));
comments.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
}
return comments;
}
public void addComment(Comment comment) {
String sql = "INSERT INTO comment (author, content) VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, comment.getAuthor());
stmt.setString(2, comment.getContent());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}}
创建两个Servlet:一个用于显示评论页面,另一个处理提交评论。
ShowCommentServlet 从数据库读取所有评论并转发到JSP页面:
@WebServlet("/show")
public class ShowCommentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
CommentDao dao = new CommentDao();
List<Comment> comments = dao.getAllComments();
request.setAttribute("comments", comments);
request.getRequestDispatcher("comment.jsp").forward(request, response);
}
}
AddCommentServlet 接收表单数据并保存:
@WebServlet("/add")
public class AddCommentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String author = request.getParameter("author");
String content = request.getParameter("content");
<pre class='brush:java;toolbar:false;'> Comment comment = new Comment();
comment.setAuthor(author);
comment.setContent(content);
CommentDao dao = new CommentDao();
dao.addComment(comment);
response.sendRedirect("show");
}}
创建comment.jsp展示评论列表和输入表单:
<h2>发表评论</h2>
<form action="add" method="post">
作者: <input type="text" name="author" required><br>
内容: <textarea name="content" rows="4" cols="50" required></textarea><br>
<input type="submit" value="提交">
</form>
<p><h2>所有评论</h2>
<% List<Comment> comments = (List<Comment>) request.getAttribute("comments");
if (comments != null) {
for (Comment c : comments) { %>
<div style="border-bottom:1px solid #ccc;padding:10px;">
<strong><%= c.getAuthor() %></strong>
<small>(<%= c.getCreateTime() %>)</small><br>
<p><%= c.getContent() %></p>
</div>
<% } } %></p>部署项目到Tomcat,确保WEB-INF下有lib目录包含mysql-connector-java和servlet-api.jar。
基本上就这些。这个简易系统展示了Java Web开发的几个关键点:数据库交互、Servlet控制流程、JSP动态输出。后续可扩展验证码、字段校验或分页功能。不复杂但容易忽略编码和SQL安全问题,建议后期引入PreparedStatement防注入,统一设置字符集避免乱码。
以上就是在Java中如何开发简易博客评论系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号