数据库表设计及创建

    实现用户注册

    实现用户登录

    实现用户列表显示

    实现用户删除与恢复

    实现用户信息显示

    实现用户信息更新与重置密码

实现用户删除与恢复功能


    这版块内容比较多,用户删除的部分页面代码请看用户列表显示userlist.jsp。


首先介绍删除功能:

批量删除,DeleteUserListServlet.java

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import entity.User;
/**
 * 用户批量删除Servlet
 */
@WebServlet("/DeleteUserListServlet")
public class DeleteUserListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
//获取要删除的用户ID
String[] userId = req.getParameterValues("num");
UserDao ud = new UserDao();
ud.deleteUserList(userId);//执行删除操作
//更新用户管理界面
ArrayList<User> list = ud.selectNotDeleteList();
req.setAttribute("list", list);
String msg = "用户已删除!";
req.setAttribute(msg, msg);
String path = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path).forward(req, resp);;
}
}

单用户删除,UserDeleteServlet.java

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import entity.User;
/**
 * 用户单个删除Servlet
 */
@WebServlet("/UserDeleteServlet")
public class UserDeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
//获取要删除的用户ID
int userId = Integer.parseInt(req.getParameter("userId"));
UserDao ud = new UserDao();
ud.deleteOneUser(userId);//执行删除操作
//显示删除后的用户列表
ArrayList<User> list = ud.selectNotDeleteList();
req.setAttribute("list", list);
String msg = "用户已删除!";
req.setAttribute(msg, msg);
String path = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path).forward(req, resp);;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
}

数据库操作--用户批量删除方法

/**
 * 批量删除用户
 * @param userId 用户ID
 */
public void deleteUserList(String[] userId) {
Connection conn = null;
PreparedStatement ps = null;
//将用户数据写入数据库
try {
conn = DBUtils.getConnection();//获取连接对象Connection
int Id = 0;
for (int i = 0; i < userId.length; i++) {
Id = Integer.parseInt(userId[i]);
String sql = "UPDATE users SET user_display='off' WHERE user_id=?";
ps = conn.prepareStatement(sql);//格式化sql语句
//为?赋值
ps.setInt(1, Id);
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
DBUtils.closeAll(null, ps, conn);
}
}

数据库操作--单用户删除方法

/**
 * 删除单个用户
 * @param userId 用户ID
 */
public void deleteOneUser(int userId) {
Connection conn = null;
PreparedStatement ps = null;
//将用户数据写入数据库
try {
conn = DBUtils.getConnection();//获取连接对象Connection
String sql = "UPDATE users SET user_display='off' WHERE user_id=?";
ps = conn.prepareStatement(sql);//格式化sql语句
//为?赋值
ps.setInt(1, userId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
DBUtils.closeAll(null, ps, conn);
}
}


其次,介绍用户恢复功能:

用户恢复JSP页面,userrecovery.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户恢复界面</title>
<style type="text/css">
td{width: 100px}
</style>
<script type="text/javascript">
//全选方法 
function chickAll() {
var chickobj = document.getElementsByName("num");
for (var i = 0; i < chickobj.length; i++) {
chickobj[i].checked = "checked";
}
}
//反选方法 
function Nochick() {
var chickobj = document.getElementsByName("num");
for (var i = 0; i < chickobj.length; i++) {
chickobj[i].checked = !chickobj[i].checked;
}
}
</script>
</head>
<body>
<div id="main">
<form name="form1" action ="UserRecoverServlet" method = "post">
<table border ="1" align = "center" style="border-collapse:collapse;">
<tr>
<td></td>
<td>用户名</td>
<td>用户账号</td>
</tr>
<c:forEach var="st" items="${list}">
<tr align="center">
<td><input type="checkbox" value='${st.userId }' name="num"></td>
<td><a href="ViewUserServlet?userId=${st.userId }">${st.userName }</a></td>
<td>${st.userAccount }</td>
</tr>
</c:forEach>
</table>
<table align="center">
<tr>
<td><input type="button" value="全选" name="checkall" οnclick="chickAll()"></td>
<td><input type="button" value="反选" name="nocheck" οnclick="Nochick()"></td>
<td><input type="submit" value="恢复用户"></td>
</tr>
</table>
<table align="center">
<tr>
<td align="center"><font color="RED">
<%
String msg = (String) request.getAttribute("msg");
if(msg != null){
out.println(msg);
}
%>
</font></td>
</tr>
</table>
</form>
</div>
</body>
</html>

用户恢复Servlet,UserRecoverServlet.java

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import entity.User;
/**
 * 恢复已删除用户Servlet
 */
@WebServlet("/UserRecoverServlet")
public class UserRecoverServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
UserDao ud = new UserDao();
ArrayList<User> list = ud.selectDeleteList();
req.setAttribute("list", list);
String msg = "如果没有找到您的用户信息,肯能是超过保留时间1年了。您或许需要重新注册呢。";
req.setAttribute(msg, msg);
String path1 = resp.encodeURL("userrecovery.jsp");
req.getRequestDispatcher(path1).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
//获取要恢复的用户ID
String[] userId = req.getParameterValues("num");
UserDao ud = new UserDao();
ud.recoveryUserList(userId);//执行恢复操作
//更新用户管理界面
ArrayList<User> list = ud.selectNotDeleteList();
req.setAttribute("list", list);
String msg = "用户已恢复,请查看,谢谢!";
req.setAttribute(msg, msg);
String path = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path).forward(req, resp);;
}
}

数据库操作--用户恢复方法

/**
 * 恢复用户
 * @param userId 用户ID
 */
public void recoveryUserList(String[] userId) {
Connection conn = null;
PreparedStatement ps = null;
//将用户数据写入数据库
try {
conn = DBUtils.getConnection();//获取连接对象Connection
int Id = 0;
for (int i = 0; i < userId.length; i++) {
Id = Integer.parseInt(userId[i]);
String sql = "UPDATE users SET user_display='on' WHERE user_id=?";
ps = conn.prepareStatement(sql);//格式化sql语句
//为?赋值
ps.setInt(1, Id);
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
DBUtils.closeAll(null, ps, conn);
}
}


至此,用户删除与恢复功能实现完成,期待未来更完善的代码和功能。