From ccb9e8e281b4fae820b829667b69565fd37c076b Mon Sep 17 00:00:00 2001 From: zhandongyang Date: Wed, 22 Jun 2022 16:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 + .../interaction/OnlnSuggestionMsgServlet.java | 1 + .../servlet/usrManagement/DeleteServlet.java | 29 ++++++++++++-- .../servlet/usrManagement/LoginServlet.java | 29 ++++++++++++-- .../usrManagement/RegisterServlet.java | 19 ++++++--- .../servlet/usrManagement/UpdateServlet.java | 40 ++++++++++++------- src/main/resources/druid.properties | 5 +-- 7 files changed, 94 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 57a6c0f..ab5c0e7 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,8 @@ ruralNetworkCulturalServInfoPlatform 1.0-SNAPSHOT + jar + 8 8 diff --git a/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java b/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java index 6b3262a..d1251e9 100644 --- a/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java +++ b/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java @@ -19,6 +19,7 @@ import java.sql.Connection; import static utils.ServletUtils.getJsonData; import static utils.ServletUtils.respWrite; +// 网上建议留言 @WebServlet(name = "OnlnSuggestionMsgServlet", value = "/comminicate/onlnSuggestionMsg") public class OnlnSuggestionMsgServlet extends HttpServlet { @Override diff --git a/src/main/java/servlet/usrManagement/DeleteServlet.java b/src/main/java/servlet/usrManagement/DeleteServlet.java index bd46347..91fd739 100644 --- a/src/main/java/servlet/usrManagement/DeleteServlet.java +++ b/src/main/java/servlet/usrManagement/DeleteServlet.java @@ -29,40 +29,63 @@ public class DeleteServlet extends HttpServlet { String code = CodeMsg.success; // 提示信息 String message = CodeMsg.successMsg; + // 响应 PrintWriter respWriter = null; try { respWriter = response.getWriter(); } catch (IOException e) { + // 将错误写入日志并返回 CusLogger.saveError(e.getMessage()); return; } + // 获取json类型的请求数据 JSONObject loginData = getJsonData(request); - String tel = loginData.getString("tel"); - String pwd = loginData.getString("pwd"); - if ("".equals(tel) || "".equals(pwd)) { + // 电话号码、密码初始化 + String tel; + String pwd; + try { + tel = loginData.getString("tel"); + pwd = loginData.getString("pwd"); + } catch (NumberFormatException e) { + // 字段缺失,将错误写入到日志,并返回响应 code = CodeMsg.fieldIsMissing; message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + respWrite(code, message, null, respWriter); + return; + } catch (Exception e) { + // 其他未知错误则为添加失败 + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(message); respWrite(code, message, null, respWriter); return; } + // 密码加密 pwd = EncryptUtil.encrypt(pwd); Connection conn = JDBCUtils.getConnectionDruid(); + // 获取要删除的用户 User user = DaoFactory.getUserDaoInstance().login(conn, tel); if (user == null) { + // 不存在此用户 code = CodeMsg.usrDoesNotExist; message = CodeMsg.usrDoesNotExistMsg; } else if (!user.getPwd().equals(pwd)) { + // 输入的用户密码错误 code = CodeMsg.wrongPwd; message = CodeMsg.wrongPwdMsg; } else { + // 删除 int delete = DaoFactory.getUserDaoInstance().delete(conn, tel); JDBCUtils.close(conn,null,null); if(delete > 0) { + // 删除成功 code = CodeMsg.success; message = CodeMsg.successMsg; respWrite(code, message, user, respWriter); return; } + // 删除失败 code = CodeMsg.deleteFailed; message = CodeMsg.deleteFailedMsg; } diff --git a/src/main/java/servlet/usrManagement/LoginServlet.java b/src/main/java/servlet/usrManagement/LoginServlet.java index c5ea4d6..75c9331 100644 --- a/src/main/java/servlet/usrManagement/LoginServlet.java +++ b/src/main/java/servlet/usrManagement/LoginServlet.java @@ -34,6 +34,7 @@ public class LoginServlet extends HttpServlet { String code = CodeMsg.success; // 提示信息 String message = CodeMsg.successMsg; + // 响应初始化 PrintWriter respWriter = null; try { respWriter = response.getWriter(); @@ -41,26 +42,46 @@ public class LoginServlet extends HttpServlet { CusLogger.saveError(e.getMessage()); return; } + // 获取请求中的json数据 JSONObject loginData = getJsonData(request); - String tel = loginData.getString("tel"); - String pwd = loginData.getString("pwd"); - if ("".equals(tel) || "".equals(pwd)) { + // 电话号码、密码初始化 + String tel; + String pwd; + // 尝试从json类型的请求数据中获取参数 + try { + tel = loginData.getString("tel"); + pwd = loginData.getString("pwd"); + } catch (NumberFormatException e) { + // 字段缺失,将错误写入到日志,并返回响应 code = CodeMsg.fieldIsMissing; message = CodeMsg.fieldIsMissingMsg; - respWrite(code,message,null,respWriter); + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + respWrite(code, message, null, respWriter); + return; + } catch (Exception e) { + // 其他未知错误则为添加失败 + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(message); + respWrite(code, message, null, respWriter); return; } + // 密码加密 pwd = EncryptUtil.encrypt(pwd); Connection conn = JDBCUtils.getConnectionDruid(); + // 根据电话号码获取正确的用户信息user User user = DaoFactory.getUserDaoInstance().login(conn,tel); JDBCUtils.close(conn,null,null); if (user == null) { + // user为空说明不存在此用户 code = CodeMsg.usrDoesNotExist; message = CodeMsg.usrDoesNotExistMsg; } else if (!user.getPwd().equals(pwd)) { + // 密码错误 code = CodeMsg.wrongPwd; message = CodeMsg.wrongPwdMsg; } else { + // 成功 code = CodeMsg.success; message = CodeMsg.successMsg; respWrite(code,message,user,respWriter); diff --git a/src/main/java/servlet/usrManagement/RegisterServlet.java b/src/main/java/servlet/usrManagement/RegisterServlet.java index d3f3380..901c3d0 100644 --- a/src/main/java/servlet/usrManagement/RegisterServlet.java +++ b/src/main/java/servlet/usrManagement/RegisterServlet.java @@ -36,15 +36,18 @@ public class RegisterServlet extends HttpServlet { String code = CodeMsg.success; // 提示信息 String message = CodeMsg.successMsg; + // 响应 PrintWriter respWriter = null; try { respWriter = response.getWriter(); } catch (IOException e) { + // 将错误写入到日志并返回 CusLogger.saveError(e.getMessage()); return; } // 获取请求参数(json类型) JSONObject registerData = getJsonData(request); + // 初始化参数,电话号码、姓名、密码、性别、年龄、就业状态(1就业、2无业)、职业名称(没有职位为无业)、年收入、是否为流动人口、住址 String tel = ""; String name = ""; String pwd = ""; @@ -55,6 +58,7 @@ public class RegisterServlet extends HttpServlet { int incm = 0; int whetherTheFloatingPopulation = 1; String address = ""; + // 尝试从json类型的请求数据中获取用户信息 try { tel = registerData.getString("tel"); name = registerData.getString("name"); @@ -67,24 +71,22 @@ public class RegisterServlet extends HttpServlet { whetherTheFloatingPopulation = registerData.getInt("whetherTheFloatingPopulation"); address = registerData.getString("address"); } catch (NumberFormatException e) { + // 字段缺失,将错误写入到日志,并返回响应 code = CodeMsg.fieldIsMissing; message = CodeMsg.fieldIsMissingMsg; CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); respWrite(code, message, null, respWriter); return; } catch (Exception e) { + // 其他未知错误则为添加失败 code = CodeMsg.addFailed; message = CodeMsg.addFailedMsg; CusLogger.saveInfo(message); respWrite(code, message, null, respWriter); return; } - // 数据验证 - if ("".equals(tel) || "".equals(name) || "".equals(pwd) || "".equals(gender) || "".equals(profession)) { - code = CodeMsg.fieldIsMissing; - message = CodeMsg.fieldIsMissingMsg; - CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); - } else if (age < 0 || incm < 0) { + // 数据验证,年龄和年收入不能少于0 + if (age < 0 || incm < 0) { code = CodeMsg.addFailed; message = CodeMsg.addFailedMsg; CusLogger.saveInfo(message); @@ -94,23 +96,28 @@ public class RegisterServlet extends HttpServlet { // user user = new User(tel, name, pwd, gender, age, emplmtState, profession, incm,whetherTheFloatingPopulation,address); Connection conn = JDBCUtils.getConnectionDruid(); + // 调用dao层函数注册用户 int res = DaoFactory.getUserDaoInstance().register(conn, user); switch (res) { case 0: CusLogger.saveUserLog(user + "注册成功"); break; case 1: + // 用户已存在 code = CodeMsg.existingPhnNum; message = CodeMsg.existingPhnNumMsg; CusLogger.saveInfo(tel + message); break; default: + // 添加失败 code = CodeMsg.addFailed; message = CodeMsg.addFailedMsg; CusLogger.saveInfo(user + message); } + // 关闭数据库连接 JDBCUtils.close(conn, null, null); } + // 调用respWrite函数,返回响应 respWrite(code, message, user, respWriter); } } diff --git a/src/main/java/servlet/usrManagement/UpdateServlet.java b/src/main/java/servlet/usrManagement/UpdateServlet.java index be6a982..0bc7e45 100644 --- a/src/main/java/servlet/usrManagement/UpdateServlet.java +++ b/src/main/java/servlet/usrManagement/UpdateServlet.java @@ -8,9 +8,11 @@ import utils.CodeMsg; import utils.EncryptUtil; import utils.JDBCUtils; -import javax.servlet.*; -import javax.servlet.http.*; -import javax.servlet.annotation.*; +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 java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; @@ -22,12 +24,12 @@ import static utils.ServletUtils.respWrite; public class UpdateServlet extends HttpServlet { @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - doPost(req,resp); + doPost(req, resp); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - doPost(request,response); + doPost(request, response); } @Override @@ -37,6 +39,7 @@ public class UpdateServlet extends HttpServlet { String code = CodeMsg.success; // 提示信息 String message = CodeMsg.successMsg; + // 响应 PrintWriter respWriter = null; try { respWriter = response.getWriter(); @@ -46,7 +49,8 @@ public class UpdateServlet extends HttpServlet { } // 获取请求参数(json类型) JSONObject registerData = getJsonData(request); - String preTel = ""; + // 初始化用户数据,修改前手机号、电话号码、姓名、密码、性别、年龄、就业状态(1就业、2无业)、职业名称(没有职位为无业)、年收入、是否为流动人口、住址 + String preTel = ""; // 修改前用户的手机号,手机号为主键 String tel = ""; String name = ""; String pwd = ""; @@ -57,6 +61,7 @@ public class UpdateServlet extends HttpServlet { int incm = 0; int whetherTheFloatingPopulation = 1; String address = ""; + // 尝试从json类型的请求数据中获取参数 try { preTel = registerData.getString("preTel"); tel = registerData.getString("tel"); @@ -70,25 +75,23 @@ public class UpdateServlet extends HttpServlet { whetherTheFloatingPopulation = registerData.getInt("whetherTheFloatingPopulation"); address = registerData.getString("address"); } catch (NumberFormatException e) { + // 字段缺失 code = CodeMsg.fieldIsMissing; message = CodeMsg.fieldIsMissingMsg; CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); respWrite(code, message, null, respWriter); return; } catch (Exception e) { + // 其他错误,返回更新失败的响应信息 code = CodeMsg.updateFailed; message = CodeMsg.updateFailedMsg; CusLogger.saveInfo(message); CusLogger.saveError(e.toString()); - respWrite(code,message,null,respWriter); + respWrite(code, message, null, respWriter); return; } - // 数据验证 - if ("".equals(tel) || "".equals(name) || "".equals(pwd) || "".equals(gender) || "".equals(profession) || "".equals(address)) { - code = CodeMsg.fieldIsMissing; - message = CodeMsg.fieldIsMissingMsg; - CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); - } else if (age < 0 || incm < 0) { + // 数据验证,年龄和年收入不能少于0 + if (age < 0 || incm < 0) { code = CodeMsg.addFailed; message = CodeMsg.addFailedMsg; CusLogger.saveInfo(message); @@ -96,18 +99,25 @@ public class UpdateServlet extends HttpServlet { // 密码加密 pwd = EncryptUtil.encrypt(pwd); // user - user = new User(tel, name, pwd, gender, age, emplmtState, profession, incm,whetherTheFloatingPopulation,address); + user = new User(tel, name, pwd, gender, age, emplmtState, profession, incm, whetherTheFloatingPopulation, address); Connection conn = JDBCUtils.getConnectionDruid(); - int res = DaoFactory.getUserDaoInstance().update(conn, preTel,user); + // 更新用户 + int res = DaoFactory.getUserDaoInstance().update(conn, preTel, user); JDBCUtils.close(conn, null, null); if (res == 0) { + // 更新成功 code = CodeMsg.success; message = CodeMsg.successMsg; respWrite(code, message, user, respWriter); return; } else if (res == 2) { + // 用户不存在 code = CodeMsg.usrDoesNotExist; message = CodeMsg.usrDoesNotExistMsg; + } else { + code = CodeMsg.updateFailed; + message = CodeMsg.updateFailedMsg; + CusLogger.saveInfo(message); } } respWrite(code, message, null, respWriter); diff --git a/src/main/resources/druid.properties b/src/main/resources/druid.properties index bcf5ade..269bbc2 100644 --- a/src/main/resources/druid.properties +++ b/src/main/resources/druid.properties @@ -3,10 +3,9 @@ username=root password=123456 driverClassName=com.mysql.cj.jdbc.Driver -initialSize=10 +initialSize=5 maxActive=20 -maxWait=1000 -filters=wall +maxWait=3000 #druid recycle Druid的连接回收机制 -- Gitee