pull/1/head
puxcofief 8 months ago
parent 5d8c73cfc8
commit 1aa25e5d25

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