diff --git a/pom.xml b/pom.xml index 57a6c0fed66ef7de30d608e443b453b3e362eec0..ab5c0e7968f6af9ad21f62b7a8574dff9ac15b96 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 6b3262af1177133c338be77a492fe2bdbd125053..d1251e9e57f7018a79838b1d3cc894a813a5717a 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 bd46347b138bc67f8164854d56d91cc5aca3b092..91fd7391fc4e5889118d2c494539fb8bbaf45f21 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 c5ea4d696c9cb47bbd97547a69627d3a042c6a02..75c9331a724a588c4f1a2ed2399f357095a895c7 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 d3f3380c69dbc1f90234f19311b4bd3b5ed5dcdd..901c3d0c057cf2eceeccf52baae30b144e4bbde1 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 be6a9824e0f016b05893c46a59c53a78c08c23c9..0bc7e457ff8db59bbcdd0e765db695e33bcaabdf 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 bcf5ade3f75e8679c7815b905429476923bfd59e..269bbc21b225f838252280dcd28e5c5618d0d325 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的连接回收机制