|
|
|
@ -1,64 +1,68 @@
|
|
|
|
|
package com.cn.servlet;
|
|
|
|
|
package com.cn.servlet; // 定义Servlet所在的包名
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.IOException; // 导入IOException,用于处理输入输出异常
|
|
|
|
|
import java.io.PrintWriter; // 导入PrintWriter,用于向客户端发送字符文本数据
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.servlet.ServletException; // 导入Servlet异常,用于处理Servlet运行时的异常
|
|
|
|
|
import javax.servlet.http.HttpServlet; // 导入HttpServlet,是所有HTTP servlet的父类
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; // 导入HttpServletRequest,代表客户端的请求信息
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; // 导入HttpServletResponse,代表服务器对客户端的响应
|
|
|
|
|
|
|
|
|
|
import com.cn.domain.Admin;
|
|
|
|
|
import com.cn.service.AdminService;
|
|
|
|
|
import com.cn.service.impl.AdminServiceImpl;
|
|
|
|
|
import com.cn.util.DateUtil;
|
|
|
|
|
import com.cn.domain.Admin; // 导入Admin类,该类定义了管理员的数据结构
|
|
|
|
|
import com.cn.service.AdminService; // 导入AdminService接口,该接口定义了管理员服务的方法
|
|
|
|
|
import com.cn.service.impl.AdminServiceImpl; // 导入AdminService接口的实现类,用于具体的管理员业务操作
|
|
|
|
|
import com.cn.util.DateUtil; // 导入DateUtil类,提供日期时间工具方法
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @ClassName: AddAdminServlet
|
|
|
|
|
* @Description: 管理员 添加管理员操作
|
|
|
|
|
* @author: ljy
|
|
|
|
|
* @date: 2019年11月10日 下午11:47:32
|
|
|
|
|
* AddAdminServlet类,用于处理添加管理员的操作。
|
|
|
|
|
* @ClassName: AddAdminServlet 类名:AddAdminServlet
|
|
|
|
|
* @Description: 管理员添加操作的Servlet
|
|
|
|
|
* @author: ljy Servlet的作者
|
|
|
|
|
* @date: 2019年11月10日 下午11:47:32 Servlet创建的日期和时间
|
|
|
|
|
*/
|
|
|
|
|
public class AddAdminServlet extends HttpServlet {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L; // 用于序列化
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理POST请求的方法。
|
|
|
|
|
* @param request HttpServletRequest对象,包含客户端的请求信息
|
|
|
|
|
* @param response HttpServletResponse对象,包含服务器对客户端的响应信息
|
|
|
|
|
* @throws ServletException 抛出Servlet异常
|
|
|
|
|
* @throws IOException 抛出输入输出异常
|
|
|
|
|
*/
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
String userName = request.getParameter("userName");
|
|
|
|
|
String password = request.getParameter("password");
|
|
|
|
|
Integer flag = Integer.valueOf(request.getParameter("flag"));
|
|
|
|
|
String userName = request.getParameter("userName"); // 从请求中获取管理员用户名参数
|
|
|
|
|
String password = request.getParameter("password"); // 从请求中获取管理员密码参数
|
|
|
|
|
Integer flag = Integer.valueOf(request.getParameter("flag")); // 从请求中获取管理员标志参数,并转换为Integer类型
|
|
|
|
|
|
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
AdminService adminService = new AdminServiceImpl();
|
|
|
|
|
PrintWriter out = response.getWriter(); // 获取PrintWriter对象,用于向客户端发送响应
|
|
|
|
|
AdminService adminService = new AdminServiceImpl(); // 创建AdminService的实现类对象,用于管理员业务操作
|
|
|
|
|
|
|
|
|
|
// 判断此用户名是否已存在
|
|
|
|
|
if(userName!=null && !"".equals(userName) && adminService.getAdminByName(userName)==null) {
|
|
|
|
|
//不存在
|
|
|
|
|
Admin admin = new Admin();
|
|
|
|
|
admin.setUserName(userName);
|
|
|
|
|
admin.setPassword(password);
|
|
|
|
|
admin.setFlag(flag);
|
|
|
|
|
admin.setIsUse(0);
|
|
|
|
|
admin.setCreatTime(DateUtil.now());
|
|
|
|
|
admin.setLoginTime(DateUtil.now());
|
|
|
|
|
// 判断用户名是否不为空且该用户名的管理员不存在
|
|
|
|
|
if(userName != null && !"".equals(userName) && adminService.getAdminByName(userName) == null) {
|
|
|
|
|
// 用户名不存在,可以添加管理员
|
|
|
|
|
Admin admin = new Admin(); // 创建Admin对象,用于封装管理员信息
|
|
|
|
|
admin.setUserName(userName); // 设置管理员用户名
|
|
|
|
|
admin.setPassword(password); // 设置管理员密码
|
|
|
|
|
admin.setFlag(flag); // 设置管理员标志
|
|
|
|
|
admin.setIsUse(0); // 设置管理员是否启用,默认为0(未启用)
|
|
|
|
|
admin.setCreatTime(DateUtil.now()); // 设置管理员创建时间,使用DateUtil.now()获取当前时间
|
|
|
|
|
admin.setLoginTime(DateUtil.now()); // 设置管理员登录时间,使用DateUtil.now()获取当前时间
|
|
|
|
|
|
|
|
|
|
int recordNumber = adminService.addAdmin(admin);
|
|
|
|
|
if(recordNumber == 1) {
|
|
|
|
|
out.write("<script>alert('添加管理员成功!');"
|
|
|
|
|
int recordNumber = adminService.addAdmin(admin); // 调用AdminService的addAdmin方法添加管理员,并返回影响的记录数
|
|
|
|
|
if(recordNumber == 1) { // 如果添加成功(影响的记录数为1)
|
|
|
|
|
out.write("<script>alert('添加管理员成功!');" // 向客户端发送JavaScript代码,弹出提示并跳转到管理员列表页面
|
|
|
|
|
+ "window.location.href='GetAllAdminServlet'</script>");
|
|
|
|
|
} else {
|
|
|
|
|
out.write("<script>alert('添加管理员失败!');"
|
|
|
|
|
} else { // 如果添加失败
|
|
|
|
|
out.write("<script>alert('添加管理员失败!');" // 向客户端发送JavaScript代码,弹出提示并跳转到管理员列表页面
|
|
|
|
|
+ "window.location.href='GetAllAdminServlet'</script>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 存在
|
|
|
|
|
out.write("<script>alert('该用户名已存在,请重新输入!');"
|
|
|
|
|
} else { // 如果用户名已存在
|
|
|
|
|
out.write("<script>alert('该用户名已存在,请重新输入!');" // 向客户端发送JavaScript代码,弹出提示并跳转到添加管理员页面
|
|
|
|
|
+ "window.location.href='pages/admin/right/addAdmin.jsp'</script>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|