实现论坛管理,1、在mysql中创建论坛表,包含留言时间、留言IP地址、留言内容,2、在Servlet程序中实现发表留言、留言显示、删除留言。3、实现留言查询(可选)。
时间: 2024-05-10 08:21:25 浏览: 51
包含登陆,发布留言,获取IP和时间,可删除的php+mysql留言簿
4星 · 用户满意度95%
1. 在MySQL中创建论坛表:
```sql
CREATE TABLE forum (
id INT(11) NOT NULL AUTO_INCREMENT,
time DATETIME NOT NULL,
ip VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 在Servlet程序中实现发表留言、留言显示、删除留言:
```java
public class ForumServlet extends HttpServlet {
private Connection conn;
public void init() throws ServletException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/forum";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void destroy() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
addMessage(request, response);
} else if ("delete".equals(action)) {
deleteMessage(request, response);
} else {
showMessages(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private void addMessage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String content = request.getParameter("content");
if (content != null && !content.isEmpty()) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO forum (time, ip, content) VALUES (NOW(), ?, ?)");
ps.setString(1, request.getRemoteAddr());
ps.setString(2, content);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect(request.getContextPath() + "/ForumServlet");
}
private void showMessages(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM forum ORDER BY time DESC");
ResultSet rs = ps.executeQuery();
List<Map<String, Object>> messages = new ArrayList<>();
while (rs.next()) {
Map<String, Object> message = new HashMap<>();
message.put("id", rs.getInt("id"));
message.put("time", rs.getTimestamp("time"));
message.put("ip", rs.getString("ip"));
message.put("content", rs.getString("content"));
messages.add(message);
}
request.setAttribute("messages", messages);
request.getRequestDispatcher("/forum.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void deleteMessage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String idStr = request.getParameter("id");
if (idStr != null && !idStr.isEmpty()) {
try {
int id = Integer.parseInt(idStr);
PreparedStatement ps = conn.prepareStatement("DELETE FROM forum WHERE id = ?");
ps.setInt(1, id);
ps.executeUpdate();
} catch (NumberFormatException | SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect(request.getContextPath() + "/ForumServlet");
}
}
```
3. 实现留言查询:
```java
private void showMessages(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String keyword = request.getParameter("keyword");
try {
PreparedStatement ps;
if (keyword != null && !keyword.isEmpty()) {
ps = conn.prepareStatement("SELECT * FROM forum WHERE content LIKE ? ORDER BY time DESC");
ps.setString(1, "%" + keyword + "%");
} else {
ps = conn.prepareStatement("SELECT * FROM forum ORDER BY time DESC");
}
ResultSet rs = ps.executeQuery();
List<Map<String, Object>> messages = new ArrayList<>();
while (rs.next()) {
Map<String, Object> message = new HashMap<>();
message.put("id", rs.getInt("id"));
message.put("time", rs.getTimestamp("time"));
message.put("ip", rs.getString("ip"));
message.put("content", rs.getString("content"));
messages.add(message);
}
request.setAttribute("messages", messages);
request.getRequestDispatcher("/forum.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
```
阅读全文