idea上使用jsp和jdbc实现班级留言 要求 1.可以实现用户的注册和登录 2.用户可以留言以及删除自己的留言 写出相关代码
时间: 2023-12-03 12:42:30 浏览: 78
以下是一个简单的实现班级留言功能的JSP页面,使用JDBC连接数据库,实现用户注册、登录、留言、删除留言等功能。
注意:为了简化代码,没有对SQL注入等安全性问题进行处理,请勿直接用于生产环境。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>班级留言板</title>
</head>
<body>
<%-- 注册 --%>
<h2>用户注册</h2>
<form action="register.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="注册">
</form>
<%-- 登录 --%>
<h2>用户登录</h2>
<form action="login.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
<%-- 留言 --%>
<hr>
<h2>留言板</h2>
<%
// 连接数据库
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/message_board";
String user = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// 处理留言提交
if (request.getMethod().equals("POST") && request.getParameter("content") != null) {
String content = request.getParameter("content");
int userId = (int)session.getAttribute("userId");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO messages (user_id, content) VALUES (?, ?)");
pstmt.setInt(1, userId);
pstmt.setString(2, content);
pstmt.executeUpdate();
}
// 显示留言列表
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM messages");
while (rs.next()) {
int messageId = rs.getInt("id");
int messageUserId = rs.getInt("user_id");
String messageContent = rs.getString("content");
String messageTime = rs.getString("time");
String messageUsername = "";
Statement stmt2 = conn.createStatement();
ResultSet rs2 = stmt2.executeQuery("SELECT * FROM users WHERE id=" + messageUserId);
if (rs2.next()) {
messageUsername = rs2.getString("username");
}
rs2.close();
stmt2.close();
out.println("<p><b>" + messageUsername + ":</b> " + messageContent + " [" + messageTime + "]");
if (session.getAttribute("userId") != null && messageUserId == (int)session.getAttribute("userId")) {
out.println(" <a href=\"delete.jsp?id=" + messageId + "\">删除</a>");
}
out.println("</p>");
}
rs.close();
stmt.close();
conn.close();
%>
<%-- 发表留言 --%>
<hr>
<h3>发表留言</h3>
<% if (session.getAttribute("userId") != null) { %>
<form action="index.jsp" method="post">
<textarea name="content" rows="5" cols="60"></textarea><br>
<input type="submit" value="发表">
</form>
<% } else { %>
<p>请先<a href="login.jsp">登录</a>后发表留言。</p>
<% } %>
<%-- 退出登录 --%>
<hr>
<% if (session.getAttribute("userId") != null) { %>
<p>您已登录,<a href="logout.jsp">退出登录</a></p>
<% } %>
</body>
</html>
```
以下是用户注册的`register.jsp`页面代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/message_board";
String user = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
String username = request.getParameter("username");
String password_ = request.getParameter("password");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, password_);
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
<h2>注册成功!</h2>
<p>返回<a href="index.jsp">留言板首页</a></p>
</body>
</html>
```
以下是用户登录的`login.jsp`页面代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/message_board";
String user = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
String username = request.getParameter("username");
String password_ = request.getParameter("password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE username='" + username + "' AND password='" + password_ + "'");
if (rs.next()) {
int userId = rs.getInt("id");
session.setAttribute("userId", userId);
response.sendRedirect("index.jsp");
} else {
out.println("<h2>用户名或密码错误!</h2>");
out.println("<p>返回<a href=\"index.jsp\">留言板首页</a></p>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```
以下是删除留言的`delete.jsp`页面代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除留言</title>
</head>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/message_board";
String user = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
int messageId = Integer.parseInt(request.getParameter("id"));
int userId = (int)session.getAttribute("userId");
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM messages WHERE id=? AND user_id=?");
pstmt.setInt(1, messageId);
pstmt.setInt(2, userId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
<h2>删除成功!</h2>
<p>返回<a href="index.jsp">留言板首页</a></p>
</body>
</html>
```
阅读全文