jsp 语言 社交平台用户黑名单管理实战

JSP阿木 发布于 2025-07-03 13 次阅读


社交平台用户黑名单管理实战:JSP技术实现

在社交平台中,用户之间的互动是构建社区氛围的关键。为了维护良好的社区环境,防止恶意用户的行为,黑名单管理成为了一个重要的功能。本文将围绕社交平台用户黑名单管理这一主题,使用JSP技术进行实战开发,实现用户黑名单的添加、查询、删除等功能。

系统需求分析

在开始开发之前,我们需要对系统需求进行分析,主要包括以下几个方面:

1. 用户管理:用户可以登录、注册、修改个人信息等。

2. 黑名单管理:用户可以添加、查询、删除黑名单中的用户。

3. 数据持久化:使用数据库存储用户信息和黑名单数据。

4. 安全性:确保用户数据的安全,防止SQL注入等安全风险。

技术选型

为了实现上述需求,我们将使用以下技术:

- 前端:HTML、CSS、JavaScript

- 后端:Java、JSP、Servlet

- 数据库:MySQL

- 开发环境:Eclipse、MySQL Workbench

系统设计

数据库设计

我们需要设计数据库表结构。以下是用户表(users)和黑名单表(blocklist)的示例:

sql

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,


username VARCHAR(50) NOT NULL,


password VARCHAR(50) NOT NULL,


email VARCHAR(100),


created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP


);

CREATE TABLE blocklist (


id INT AUTO_INCREMENT PRIMARY KEY,


user_id INT NOT NULL,


blocked_user_id INT NOT NULL,


created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,


FOREIGN KEY (user_id) REFERENCES users(id),


FOREIGN KEY (blocked_user_id) REFERENCES users(id)


);


JSP页面设计

接下来,我们设计JSP页面,包括登录页面、注册页面、用户中心页面和黑名单管理页面。

登录页面(login.jsp)

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>


<html>


<head>


<title>登录</title>


</head>


<body>


<form action="loginServlet" method="post">


用户名:<input type="text" name="username" /><br/>


密码:<input type="password" name="password" /><br/>


<input type="submit" value="登录" />


</form>


</body>


</html>


注册页面(register.jsp)

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>


<html>


<head>


<title>注册</title>


</head>


<body>


<form action="registerServlet" method="post">


用户名:<input type="text" name="username" /><br/>


密码:<input type="password" name="password" /><br/>


邮箱:<input type="email" name="email" /><br/>


<input type="submit" value="注册" />


</form>


</body>


</html>


用户中心页面(user_center.jsp)

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<%@ page import="java.sql." %>


<!DOCTYPE html>


<html>


<head>


<title>用户中心</title>


</head>


<body>


<h1>用户中心</h1>


<a href="blocklist.jsp">管理黑名单</a>


<!-- 其他用户信息 -->


</body>


</html>


黑名单管理页面(blocklist.jsp)

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<%@ page import="java.sql." %>


<!DOCTYPE html>


<html>


<head>


<title>黑名单管理</title>


</head>


<body>


<h1>黑名单管理</h1>


<form action="addBlockServlet" method="post">


被封禁用户ID:<input type="text" name="blocked_user_id" /><br/>


<input type="submit" value="添加到黑名单" />


</form>


<table>


<tr>


<th>被封禁用户ID</th>


<th>操作</th>


</tr>


<%


Connection conn = null;


PreparedStatement pstmt = null;


ResultSet rs = null;


try {


conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/social_platform", "root", "password");


pstmt = conn.prepareStatement("SELECT FROM blocklist WHERE user_id = ?");


pstmt.setInt(1, session.getAttribute("user_id"));


rs = pstmt.executeQuery();


while (rs.next()) {


out.println("<tr><td>" + rs.getInt("blocked_user_id") + "</td><td><a href='deleteBlockServlet?id=" + rs.getInt("id") + "'>删除</a></td></tr>");


}


} catch (Exception e) {


e.printStackTrace();


} finally {


try {


if (rs != null) rs.close();


if (pstmt != null) pstmt.close();


if (conn != null) conn.close();


} catch (SQLException e) {


e.printStackTrace();


}


}


%>


</table>


</body>


</html>


后端实现

Servlet类

我们需要编写几个Servlet类来处理用户请求。

登录Servlet(LoginServlet.java)

java

@WebServlet("/loginServlet")


public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String username = request.getParameter("username");


String password = request.getParameter("password");


// 验证用户名和密码,此处省略


// 登录成功后,将用户信息存入session


}


}


注册Servlet(RegisterServlet.java)

java

@WebServlet("/registerServlet")


public class RegisterServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String username = request.getParameter("username");


String password = request.getParameter("password");


String email = request.getParameter("email");


// 注册用户,此处省略


}


}


添加黑名单Servlet(AddBlockServlet.java)

java

@WebServlet("/addBlockServlet")


public class AddBlockServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


int blockedUserId = Integer.parseInt(request.getParameter("blocked_user_id"));


int userId = (int) request.getSession().getAttribute("user_id");


// 添加黑名单,此处省略


}


}


删除黑名单Servlet(DeleteBlockServlet.java)

java

@WebServlet("/deleteBlockServlet")


public class DeleteBlockServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


int id = Integer.parseInt(request.getParameter("id"));


// 删除黑名单,此处省略


}


}


总结

本文通过JSP技术实现了社交平台用户黑名单管理功能。在实际开发中,我们还需要考虑更多的细节,如用户权限控制、异常处理、安全性等。通过本文的实战案例,读者可以了解到JSP在社交平台开发中的应用,并能够根据实际需求进行扩展和优化。