diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..1ab8d7d4d9937981a5d823d891270725b9f22160 --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.example + ruralNetworkCulturalServInfoPlatform + 1.0-SNAPSHOT + + + 8 + 8 + + + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + mysql + mysql-connector-java + 8.0.29 + + + + com.alibaba + druid + 1.2.9 + + + + commons-dbutils + commons-dbutils + 1.7 + + + com.google.code.gson + gson + 2.9.0 + + + log4j + log4j + 1.2.17 + + + + org.json + json + 20220320 + + + + + \ No newline at end of file diff --git a/src/main/java/bean/Merch.java b/src/main/java/bean/Merch.java new file mode 100644 index 0000000000000000000000000000000000000000..65d419e109bcd2e679c7be30fd2f43dcf5930e47 --- /dev/null +++ b/src/main/java/bean/Merch.java @@ -0,0 +1,82 @@ +package bean; + +/** + * @author zdy + */ +public class Merch { + private String name; + private String icoUrl; + private String introduction; + private String details; + private int type; + + public Merch() { + + } + + public Merch(String name, String icoUrl, String introduction, String details) { + this.name = name; + this.icoUrl = icoUrl; + this.introduction = introduction; + this.details = details; + } + + public Merch(String name, String icoUrl, String introduction, String details, int type) { + this.name = name; + this.icoUrl = icoUrl; + this.introduction = introduction; + this.details = details; + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIcoUri() { + return icoUrl; + } + + public void setIcoUri(String icoUrl) { + this.icoUrl = icoUrl; + } + + public String getIntroduction() { + return introduction; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } + + public String getDetails() { + return details; + } + + public void setDetails(String details) { + this.details = details; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + @Override + public String toString() { + return "Merch{" + + "name='" + name + '\'' + + ", icoUrl='" + icoUrl + '\'' + + ", introduction='" + introduction + '\'' + + ", details='" + details + '\'' + + ", type=" + type + + '}'; + } +} diff --git a/src/main/java/bean/OnlnServEvaluation.java b/src/main/java/bean/OnlnServEvaluation.java new file mode 100644 index 0000000000000000000000000000000000000000..792e623348f87776983fb3d1f434451fce7c71a1 --- /dev/null +++ b/src/main/java/bean/OnlnServEvaluation.java @@ -0,0 +1,49 @@ +package bean; + +public class OnlnServEvaluation { + private String project; + private String name; + private int star; + + public OnlnServEvaluation() { + } + + public OnlnServEvaluation(String project, String name, int star) { + this.project = project; + this.name = name; + this.star = star; + } + + public String getProject() { + return project; + } + + public void setProject(String project) { + this.project = project; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getStar() { + return star; + } + + public void setStar(int star) { + this.star = star; + } + + @Override + public String toString() { + return "OnlnServEvaluation{" + + "project='" + project + '\'' + + ", name='" + name + '\'' + + ", star=" + star + + '}'; + } +} diff --git a/src/main/java/bean/OnlnSuggestionMsg.java b/src/main/java/bean/OnlnSuggestionMsg.java new file mode 100644 index 0000000000000000000000000000000000000000..62bb4e91e4e351e026220be31ad373526fde3502 --- /dev/null +++ b/src/main/java/bean/OnlnSuggestionMsg.java @@ -0,0 +1,38 @@ +package bean; + +public class OnlnSuggestionMsg { + private String name; + private String content; + + public OnlnSuggestionMsg() { + } + + public OnlnSuggestionMsg(String name, String content) { + this.name = name; + this.content = content; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "OnlnSuggestionMsg{" + + "name='" + name + '\'' + + ", content='" + content + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/PubAnnouncements.java b/src/main/java/bean/PubAnnouncements.java new file mode 100644 index 0000000000000000000000000000000000000000..36881c043e64e1670482ff82a3e77587bc27e933 --- /dev/null +++ b/src/main/java/bean/PubAnnouncements.java @@ -0,0 +1,38 @@ +package bean; + +public class PubAnnouncements { + private String title; + private String content; + + public PubAnnouncements() { + } + + public PubAnnouncements(String title, String content) { + this.title = title; + this.content = content; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "PubAnnouncements{" + + "title='" + title + '\'' + + ", content='" + content + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/Resident.java b/src/main/java/bean/Resident.java new file mode 100644 index 0000000000000000000000000000000000000000..ae6e65ea5f53ec1ca9094efe3de6b3b832d31644 --- /dev/null +++ b/src/main/java/bean/Resident.java @@ -0,0 +1,61 @@ +package bean; + +public class Resident { + private String name; + private String gender; + private int age; + private String address; + + public Resident() { + + } + + public Resident(String name, String gender, int age, String address) { + this.name = name; + this.gender = gender; + this.age = age; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "Resident{" + + "name='" + name + '\'' + + ", gender='" + gender + '\'' + + ", age=" + age + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/User.java b/src/main/java/bean/User.java new file mode 100644 index 0000000000000000000000000000000000000000..1972e4aea918ad3099ef1463c1b30fcc572dc4ab --- /dev/null +++ b/src/main/java/bean/User.java @@ -0,0 +1,129 @@ +package bean; + +/** + * @author zdy + */ +public class User { + private String tel; + private String name; + private String pwd; + private String gender; + private int age; + private int emplmtState; + private String profession; + private int incm; + private int whetherTheFloatingPopulation; + private String address; + + public User() { + } + + public User(String tel, String name, String pwd, String gender, int age, int emplmtState, String profession, int incm, int whetherTheFloatingPopulation, String address) { + this.tel = tel; + this.name = name; + this.pwd = pwd; + this.gender = gender; + this.age = age; + this.emplmtState = emplmtState; + this.profession = profession; + this.incm = incm; + this.whetherTheFloatingPopulation = whetherTheFloatingPopulation; + this.address = address; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public int getEmplmtState() { + return emplmtState; + } + + public void setEmplmtState(int emplmtState) { + this.emplmtState = emplmtState; + } + + public String getProfession() { + return profession; + } + + public void setProfession(String profession) { + this.profession = profession; + } + + public int getIncm() { + return incm; + } + + public void setIncm(int incm) { + this.incm = incm; + } + + public int getWhetherTheFloatingPopulation() { + return whetherTheFloatingPopulation; + } + + public void setWhetherTheFloatingPopulation(int whetherTheFloatingPopulation) { + this.whetherTheFloatingPopulation = whetherTheFloatingPopulation; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "User{" + + "tel='" + tel + '\'' + + ", name='" + name + '\'' + + ", pwd='" + pwd + '\'' + + ", gender='" + gender + '\'' + + ", age=" + age + + ", emplmtState=" + emplmtState + + ", profession='" + profession + '\'' + + ", incm=" + incm + + ", whetherTheFloatingPopulation=" + whetherTheFloatingPopulation + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/VillageArticle.java b/src/main/java/bean/VillageArticle.java new file mode 100644 index 0000000000000000000000000000000000000000..fb707b9622d02d3c76b41343ad0c3b40342e2ab3 --- /dev/null +++ b/src/main/java/bean/VillageArticle.java @@ -0,0 +1,38 @@ +package bean; + +public class VillageArticle { + private String name; + private String url; + + public VillageArticle() { + } + + public VillageArticle(String name, String url) { + this.name = name; + this.url = url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + @Override + public String toString() { + return "VillageArticle{" + + "name='" + name + '\'' + + ", url='" + url + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/VillageGrp.java b/src/main/java/bean/VillageGrp.java new file mode 100644 index 0000000000000000000000000000000000000000..30c943c59007b1a0d7bea40c09ca3be1b61483f7 --- /dev/null +++ b/src/main/java/bean/VillageGrp.java @@ -0,0 +1,38 @@ +package bean; + +public class VillageGrp { + private String name; + private String grpNum; + + public VillageGrp() { + } + + public VillageGrp(String name, String grpNum) { + this.name = name; + this.grpNum = grpNum; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getGrpNum() { + return grpNum; + } + + public void setGrpNum(String grpNum) { + this.grpNum = grpNum; + } + + @Override + public String toString() { + return "VillageGrp{" + + "name='" + name + '\'' + + ", grpNum='" + grpNum + '\'' + + '}'; + } +} diff --git a/src/main/java/bean/VillageHotline.java b/src/main/java/bean/VillageHotline.java new file mode 100644 index 0000000000000000000000000000000000000000..94e9ac2d45c049c16d34e5a7d64d97b6a1afb2cd --- /dev/null +++ b/src/main/java/bean/VillageHotline.java @@ -0,0 +1,38 @@ +package bean; + +public class VillageHotline { + private String name; + private String tel; + + public VillageHotline() { + } + + public VillageHotline(String name, String tel) { + this.name = name; + this.tel = tel; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } + + @Override + public String toString() { + return "VillageHotline{" + + "name='" + name + '\'' + + ", tel='" + tel + '\'' + + '}'; + } +} diff --git a/src/main/java/dao/IUserDao.java b/src/main/java/dao/IUserDao.java new file mode 100644 index 0000000000000000000000000000000000000000..cffe9779a6ef0e0cd2df267d295725418d37205a --- /dev/null +++ b/src/main/java/dao/IUserDao.java @@ -0,0 +1,29 @@ +package dao; + +import bean.*; + +import java.sql.Connection; +import java.util.List; + +public interface IUserDao { + int register(Connection conn, User user); + User login(Connection conn,String tel); + int update(Connection conn,String preTel,User user); + int delete(Connection conn,String tel); + List getTheFloatingPopulation(Connection conn); + List getHlpPeople(Connection conn); + List getWomensGroups(Connection conn); + List getTheUnemployed(Connection conn); + List getOlder(Connection conn); + List getRuralMerchants(Connection conn); + List getSurroundingCompanies(Connection conn); + List getAPropertyCo(Connection conn); + int registerAsAMerch(Connection conn,Merch merch); + List getVillageGrp(Connection conn); + List getVillageArticle(Connection conn); + List getVillageHotline(Connection conn); + List getPubAnnouncements(Connection conn); + + int onlnSuggestionMsg(Connection conn,OnlnSuggestionMsg suggestionMsg); + int onlnServEvaluation(Connection conn,OnlnServEvaluation servEvaluation); +} diff --git a/src/main/java/dao/impl/MysqlDaoImpl.java b/src/main/java/dao/impl/MysqlDaoImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..1221468dbdf489a034601ead402b12a422b8b330 --- /dev/null +++ b/src/main/java/dao/impl/MysqlDaoImpl.java @@ -0,0 +1,331 @@ +package dao.impl; + +import bean.*; +import dao.IUserDao; +import log.CusLogger; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.commons.dbutils.handlers.BeanHandler; +import utils.CodeMsg; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.SQLIntegrityConstraintViolationException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author zdy + */ +public class MysqlDaoImpl implements IUserDao { + QueryRunner qr = new QueryRunner(); + + @Override + public int register(Connection conn, User user) { + String sql = "insert into rncspUser(tel,name,pwd,gender,age,emplmtState,profession,incm,whetherTheFloatingPopulation,address) values(?,?,?,?,?,?,?,?,?,?)"; + int ret = 0; + try { + ret = qr.update(conn, sql, user.getTel(), user.getName(), user.getPwd(), user.getGender(), + user.getAge(), user.getEmplmtState(), user.getProfession(), user.getIncm(),user.getWhetherTheFloatingPopulation(),user.getAddress()) <= 0 ? 1 : 0; + } catch (SQLIntegrityConstraintViolationException e) { + ret = 2; + CusLogger.saveError(CodeMsg.existingPhnNumMsg); + } catch (SQLException e) { + ret = 1; + CusLogger.saveError(e.getMessage()); + } + return ret; + } + + @Override + public User login(Connection conn, String tel) { + String sql = "select tel,name,pwd,gender,age,emplmtState,profession,incm,whetherTheFloatingPopulation,address from rncspUser where tel = ?"; + try { + return qr.query(conn, sql, new BeanHandler<>(User.class), tel); + } catch (SQLException e) { + CusLogger.saveError(e.toString()); + return null; + } + } + + @Override + public int update(Connection conn, String preTel, User user) { + int ret = 0; + String selectSql = "select tel,name,pwd,gender,age,emplmtState,profession,incm from rncspUser where tel = ?"; + String sql = "update rncspUser set tel = ?,name = ?,pwd = ?,gender = ?,age = ?,emplmtState = ?,profession = ?,incm = ? where tel = ?"; + try { + User preUser = qr.query(conn, selectSql, new BeanHandler<>(User.class), preTel); + if (preUser == null) { + ret = 2; + } else { + ret = qr.update(conn, sql, user.getTel(), user.getName(), user.getPwd(), user.getGender(), user.getAge(), user.getEmplmtState(), user.getProfession(), user.getIncm(), preTel) <= 0 ? 1 : 0; + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + @Override + public int delete(Connection conn, String tel) { + int ret = 0; + String sql = "delete from rncspUser where tel = ?"; + try { + ret = qr.update(conn,sql,tel); + } catch (SQLException e) { + CusLogger.saveError(e.toString()); + } + return ret; + } + + @Override + public List getTheFloatingPopulation(Connection conn) { + String sql = "select name,gender,age,address from rncspUser where whetherTheFloatingPopulation = 1"; + return getResident(conn, sql); + } + + @Override + public List getHlpPeople(Connection conn) { + String sql = "select name,gender,age,address from rncspUser where emplmtState = 2 and incm < 6000"; + return getResident(conn, sql); + } + + @Override + public List getWomensGroups(Connection conn) { + String sql = "select name,gender,age,address from rncspUser where gender = '女' or gender = '女性'"; + return getResident(conn, sql); + } + + @Override + public List getTheUnemployed(Connection conn) { + String sql = "select name,gender,age,address from rncspUser where emplmtState = 2"; + return getResident(conn, sql); + } + + @Override + public List getOlder(Connection conn) { + String sql = "select name,gender,age,address from rncspUser where age > 60"; + return getResident(conn, sql); + } + + @Override + public List getRuralMerchants(Connection conn) { + String sql = "select name,icoUri,introduction,details from rncspMerch where type = 1"; + return getMerch(conn, sql); + } + + @Override + public List getSurroundingCompanies(Connection conn) { + String sql = "select name,icoUri,introduction,details from rncspMerch where type = 2"; + return getMerch(conn, sql); + } + + @Override + public List getAPropertyCo(Connection conn) { + String sql = "select name,icoUri,introduction,details from rncspMerch where type = 3"; + return getMerch(conn, sql); + } + + @Override + public int registerAsAMerch(Connection conn, Merch merch) { + String sql = "insert into rncspMerch(name,icoUri,introduction,details,type) values(?,?,?,?,?)"; + int ret = 0; + try { + System.out.println(merch); + ret = qr.update(conn, sql, merch.getName(), merch.getIcoUri(), merch.getIntroduction(), merch.getDetails(), merch.getType()) <= 0 ? 1 : 0; + } catch (SQLIntegrityConstraintViolationException e) { + ret = 2; + CusLogger.saveError(CodeMsg.merchAlreadyExistsMsg); + CusLogger.saveError(e.toString()); + } catch (SQLException e) { + ret = 1; + CusLogger.saveError(CodeMsg.registerAsMerchFailedMsg); + CusLogger.saveError(e.toString()); + } + return ret; + } + + @Override + public List getVillageGrp(Connection conn) { + List ret = null; + String sql = "select name,grpNum from rncspVG"; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 1) { + continue; + } + VillageGrp villageGrp = new VillageGrp(alist[0].toString(), alist[1].toString()); + ret.add(villageGrp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + @Override + public List getVillageArticle(Connection conn) { + List ret = null; + String sql = "select name,url from rncspVA"; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 1) { + continue; + } + VillageArticle villageArticle = new VillageArticle(alist[0].toString(), alist[1].toString()); + ret.add(villageArticle); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + @Override + public List getVillageHotline(Connection conn) { + List ret = null; + String sql = "select name,tel from rncspVH"; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 1) { + continue; + } + VillageHotline villageHotline = new VillageHotline(alist[0].toString(), alist[1].toString()); + ret.add(villageHotline); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + @Override + public List getPubAnnouncements(Connection conn) { + List ret = null; + String sql = "select title,content from rncspPA"; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 1) { + continue; + } + PubAnnouncements pubAnnouncements = new PubAnnouncements(alist[0].toString(), alist[1].toString()); + ret.add(pubAnnouncements); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + @Override + public int onlnSuggestionMsg(Connection conn, OnlnSuggestionMsg suggestionMsg) { + String sql = "insert into rncspOSM(name,content) values(?,?)"; + int ret = 0; + try { + ret = qr.update(conn, sql, suggestionMsg.getName(),suggestionMsg.getContent()) <= 0 ? 1 : 0; + } catch (SQLIntegrityConstraintViolationException e) { + ret = 2; + CusLogger.saveError(CodeMsg.merchAlreadyExistsMsg); + CusLogger.saveError(e.toString()); + } catch (SQLException e) { + ret = 1; + CusLogger.saveError(CodeMsg.registerAsMerchFailedMsg); + CusLogger.saveError(e.toString()); + } + return ret; + } + + @Override + public int onlnServEvaluation(Connection conn, OnlnServEvaluation servEvaluation) { + String sql = "insert into rncspOSE(project,name,star) values(?,?,?)"; + int ret = 0; + try { + ret = qr.update(conn, sql, servEvaluation.getProject(),servEvaluation.getName(),servEvaluation.getStar()) <= 0 ? 1 : 0; + } catch (SQLIntegrityConstraintViolationException e) { + ret = 2; + CusLogger.saveError(CodeMsg.merchAlreadyExistsMsg); + CusLogger.saveError(e.toString()); + } catch (SQLException e) { + ret = 1; + CusLogger.saveError(CodeMsg.registerAsMerchFailedMsg); + CusLogger.saveError(e.toString()); + } + return ret; + } + + public List getResident(Connection conn, String sql) { + List ret = null; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 3) { + continue; + } + Resident resident = new Resident(alist[0].toString(), alist[1].toString(), Integer.parseInt(alist[2].toString()), alist[3].toString()); + ret.add(resident); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + public List getMerch(Connection conn, String sql) { + List ret = null; + try { + List list = qr.query(conn, sql, new ArrayListHandler()); + ret = new ArrayList<>(); + for (Object[] alist : list) { + if (alist.length <= 3) { + continue; + } + Merch merch = new Merch(alist[0].toString(), alist[1].toString(), alist[2].toString(), alist[3].toString()); + ret.add(merch); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ret; + } + + +// @Override +// public int register(Connection conn, User user) { +// String sql = "insert into rncspUser(tel,name,pwd,gender,age,emplmt_state,profession,incm) values(?,?,?,?,?,?,?,?)"; +// PreparedStatement ps = null; +// // ret: 0成功,1为失败,2为电话号码已经存在 +// int ret = 0; +// try { +// ps = conn.prepareStatement(sql); +// ps.setString(1, user.getTel()); +// ps.setString(2, user.getName()); +// ps.setString(3, user.getPwd()); +// ps.setString(4, user.getGender()); +// ps.setInt(5, user.getAge()); +// ps.setInt(6, user.getEmplmtState()); +// ps.setString(7, user.getProfession()); +// ps.setInt(8, user.getIncm()); +// +// ret = ps.executeUpdate() <= 0?1:0; +// } catch (SQLIntegrityConstraintViolationException e) { +// ret = 2; +// CusLogger.saveError(Message.existingPhnNumMsg); +// } catch (SQLException e) { +// ret = 1; +// CusLogger.saveError(e.getMessage()); +// } finally { +// JDBCUtils.close(null,ps,null); +// } +// return ret; +// } + +} diff --git a/src/main/java/factory/DaoFactory.java b/src/main/java/factory/DaoFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..88cf9bce0590287ca372bf36a43f399d0c11c442 --- /dev/null +++ b/src/main/java/factory/DaoFactory.java @@ -0,0 +1,13 @@ +package factory; + +import dao.IUserDao; +import dao.impl.MysqlDaoImpl; + +/** + * @author zdy + */ +public class DaoFactory { + public static IUserDao getUserDaoInstance(){ + return new MysqlDaoImpl(); + } +} diff --git a/src/main/java/filter/CharacterFilter.java b/src/main/java/filter/CharacterFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..78c4a09eb47e6920c925fd797b530ca0bc86518c --- /dev/null +++ b/src/main/java/filter/CharacterFilter.java @@ -0,0 +1,64 @@ +package filter; + + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +//@WebServlet("/characterFilter") +public class CharacterFilter implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + //解决POST请求乱码 + request.setCharacterEncoding("UTF-8"); + //解决响应乱码 + response.setContentType("text/html;charset=utf-8"); + //解决get乱码 + CharacterRequest characterRequest = new CharacterRequest(request); + filterChain.doFilter(characterRequest, response); + } + + @Override + public void destroy() { + + } + + class CharacterRequest extends HttpServletRequestWrapper { + private HttpServletRequest request; + + public CharacterRequest(HttpServletRequest request) { + super(request); + this.request = request; + } + + @Override + public String getParameter(String name) { + String value = super.getParameter(name); + if (value == null) { + return null; + } else { + String method = request.getMethod(); + if ("get".equalsIgnoreCase(method)) { + try { + value = new String(value.getBytes("iso-8859-1"), "utf-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + } + return value; + } + } +} + + diff --git a/src/main/java/filter/LoginFilter.java b/src/main/java/filter/LoginFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..b7f4911db695bb29403a8eac69494147c4b4eaf7 --- /dev/null +++ b/src/main/java/filter/LoginFilter.java @@ -0,0 +1,63 @@ +package filter; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +//@WebServlet("/loginFilter") +public class LoginFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) { + // TODO Auto-generated method stub + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + // 获得在下面代码中要用的request,response,session对象 + HttpServletRequest servletRequest = (HttpServletRequest) request; + HttpServletResponse servletResponse = (HttpServletResponse) response; + HttpSession session = servletRequest.getSession(); + + // 获得用户请求的URI + String path = servletRequest.getRequestURI(); + //System.out.println(path); + + // 从session里取用户名 + String loginName = (String) session.getAttribute("name"); + + // 登陆注册页面无需过滤 + if (path.contains("/index.jsp") || path.contains("/register.jsp") || path.contains("/registerResponse.jsp")) { + System.out.println("登录或注册页面,退出过滤"); + chain.doFilter(servletRequest, servletResponse); + return; + } + if (path.contains("/AddServlet") || path.contains("/LoginServlet")) { + System.out.println("登录或注册servlet,退出过滤"); + chain.doFilter(servletRequest, servletResponse); + return; + } + + System.out.println("访问路径:" + path + " 执行过滤"); + System.out.println("用户名:" + loginName); + // 判断如果没有取到用户信息,就跳转到登陆页面 + if (loginName == null || "".equals(loginName)) { + // 跳转到登陆页面 + servletResponse.sendRedirect("index.jsp"); + } else { + // 已经登陆,继续此次请求 + chain.doFilter(request, response); + } + + } + + @Override + public void destroy() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/listener/OnlineCountListener.java b/src/main/java/listener/OnlineCountListener.java new file mode 100644 index 0000000000000000000000000000000000000000..ccac199e302913ffe34a26b1a841b031616d86d4 --- /dev/null +++ b/src/main/java/listener/OnlineCountListener.java @@ -0,0 +1,57 @@ +package listener; + +import utils.OnLineList; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +//统计网站在线人数 +public class OnlineCountListener implements HttpSessionListener { + + //创建session时触发 + public void sessionCreated(HttpSessionEvent se) { + //获取servlet上下文对象 + ServletContext servletContext = se.getSession().getServletContext(); +// +// Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount"); +// if (onlineCount == null) { +// //如果在线人数为0,则设置在线人数为1 +// onlineCount = new Integer(1); +// } else { +// //在线人数+1 +// int count = onlineCount.intValue(); +// onlineCount = new Integer(count + 1); +// } +// //将在线人数放入servletContext对象中 +// servletContext.setAttribute("OnlineCount", onlineCount); +// String username = (String) servletContext.getAttribute("loginName"); +// OnlineList.list.add(username); + System.out.println(OnLineList.list); + } + + + //销毁session时触发 + public void sessionDestroyed(HttpSessionEvent se) { + System.out.println("会话关闭"); + //获取servlet上下文对象 + ServletContext servletContext = se.getSession().getServletContext(); +// Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount"); +// if (onlineCount == null) { +// //如果在线人数为0,则设置在线人数为0 +// onlineCount = new Integer(0); +// } else { +// //在线人数-1 +// int count = onlineCount.intValue(); +// onlineCount = new Integer(count - 1); +// } +// servletContext.setAttribute("OnlineCount", onlineCount); + String userName = (String) servletContext.getAttribute("name"); + + if (userName.length() != 0) { + OnLineList.list.remove(userName); + } + System.out.println(OnLineList.list); + } +} + diff --git a/src/main/java/log/CusLogger.java b/src/main/java/log/CusLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..7db1c21a89f0195d2f5e0ae4563b098209962349 --- /dev/null +++ b/src/main/java/log/CusLogger.java @@ -0,0 +1,51 @@ +package log; + +/** + * @author zdy + */ +public class CusLogger { + final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CusLogger.class); + final static org.apache.log4j.Logger saveUserLog = org.apache.log4j.Logger.getLogger("saveUserLog"); +// public static void main(String[] args) { +// final org.apache.log4j.CusLogger logger = org.apache.log4j.CusLogger.getLogger(CusLogger.class); +// final org.apache.log4j.CusLogger saveUserLog = org.apache.log4j.CusLogger.getLogger("saveUserLog"); +// if (logger.isDebugEnabled()) { +// logger.debug("debug"); +// } +// +// logger.info("info"); +// logger.error("error"); +// +// saveUserLog.info("张三,男,26岁,北京大学,2018-05-19,学霸"); +// } + + public static void saveInfo(String info) { +// //设定为当前文件夹 +// File directory = new File(""); +// try { +// //获取标准的路径 +// System.out.println(directory.getCanonicalPath()); +// //获取绝对路径 +// System.out.println(directory.getAbsolutePath()); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } + + if (logger.isDebugEnabled()) { + logger.debug("debug"); + } + logger.info(info); + } + + public static void saveError(String error) { + if (logger.isDebugEnabled()) { + logger.debug("debug"); + } + logger.error(error); + } + + public static void saveUserLog(String userlog) { + saveUserLog.info(userlog); + } + +} \ No newline at end of file diff --git a/src/main/java/servlet/interaction/GetPubAnnouncementsServlet.java b/src/main/java/servlet/interaction/GetPubAnnouncementsServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..452c61238e5da2af90fa20d17b76b1d599e6743a --- /dev/null +++ b/src/main/java/servlet/interaction/GetPubAnnouncementsServlet.java @@ -0,0 +1,45 @@ +package servlet.interaction; + +import bean.PubAnnouncements; +import bean.VillageArticle; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetPubAnnouncementsServlet", value = "/comminicate/getPubAnnouncements") +public class GetPubAnnouncementsServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List pubAnnouncements = DaoFactory.getUserDaoInstance().getPubAnnouncements(conn); + message = "共查询到"+pubAnnouncements.size()+"条记录"; + respWrite(code,message,pubAnnouncements,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/interaction/GetVillageArticleServlet.java b/src/main/java/servlet/interaction/GetVillageArticleServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..df956134fbc7293e4604ebd69db63a67f8ff9fe1 --- /dev/null +++ b/src/main/java/servlet/interaction/GetVillageArticleServlet.java @@ -0,0 +1,46 @@ +package servlet.interaction; + +import bean.Resident; +import bean.VillageArticle; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetVillageArticleServlet", value = "/comminicate/getVillageArticle") +public class GetVillageArticleServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List villageArticle = DaoFactory.getUserDaoInstance().getVillageArticle(conn); + message = "共查询到"+villageArticle.size()+"条记录"; + respWrite(code,message,villageArticle,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } + +} diff --git a/src/main/java/servlet/interaction/GetVillageGrpServlet.java b/src/main/java/servlet/interaction/GetVillageGrpServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..f6116f019b003991defb507b670569adb469d521 --- /dev/null +++ b/src/main/java/servlet/interaction/GetVillageGrpServlet.java @@ -0,0 +1,45 @@ +package servlet.interaction; + +import bean.Resident; +import bean.VillageGrp; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetVillageGrpServlet", value = "/comminicate/getVillageGrp") +public class GetVillageGrpServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List villageGrp = DaoFactory.getUserDaoInstance().getVillageGrp(conn); + message = "共查询到"+villageGrp.size()+"条记录"; + respWrite(code,message,villageGrp,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/interaction/GetVillageHotlineServlet.java b/src/main/java/servlet/interaction/GetVillageHotlineServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..056294268cfedb7a40317c7a0f1d855d4ff0d933 --- /dev/null +++ b/src/main/java/servlet/interaction/GetVillageHotlineServlet.java @@ -0,0 +1,45 @@ +package servlet.interaction; + +import bean.VillageArticle; +import bean.VillageHotline; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetVillageHotlineServlet", value = "/GetVillageHotlineServlet") +public class GetVillageHotlineServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List villageHotline = DaoFactory.getUserDaoInstance().getVillageHotline(conn); + message = "共查询到"+villageHotline.size()+"条记录"; + respWrite(code,message,villageHotline,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/interaction/OnlnServEvaluationServlet.java b/src/main/java/servlet/interaction/OnlnServEvaluationServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..086c8906ab7ca4b31c1c78ac6b7d130975a26f4e --- /dev/null +++ b/src/main/java/servlet/interaction/OnlnServEvaluationServlet.java @@ -0,0 +1,85 @@ +package servlet.interaction; + +import bean.OnlnServEvaluation; +import bean.OnlnSuggestionMsg; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "OnlnServEvaluationServlet", value = "/comminicate/onlnServEvaluation") +public class OnlnServEvaluationServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) { + OnlnServEvaluation servEvaluation = null; + // 状态码 + 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 servEvaluationData = getJsonData(request); + String project = ""; + String name = ""; + int star = 0; + try { + project = servEvaluationData.getString("project"); + name = servEvaluationData.getString("name"); + star = servEvaluationData.getInt("star"); + } catch (NumberFormatException e) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + respWrite(code, message, null, respWriter); + return; + } + // 数据验证 + if(star < 0 || star > 5) { + code = CodeMsg.illegalNumOfStars; + message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + } else { + servEvaluation = new OnlnServEvaluation(project,name,star); + Connection conn = JDBCUtils.getConnectionDruid(); + int res = DaoFactory.getUserDaoInstance().onlnServEvaluation(conn, servEvaluation); + switch (res) { + case 0: + CusLogger.saveUserLog(servEvaluation + "评价成功"); + break; + case 1: + code = CodeMsg.existingPhnNum; + message = CodeMsg.existingPhnNumMsg; + CusLogger.saveInfo(name + message); + break; + default: + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(name + message); + } + JDBCUtils.close(conn, null, null); + } + respWrite(code, message, servEvaluation, respWriter); + } +} diff --git a/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java b/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..6b3262af1177133c338be77a492fe2bdbd125053 --- /dev/null +++ b/src/main/java/servlet/interaction/OnlnSuggestionMsgServlet.java @@ -0,0 +1,77 @@ +package servlet.interaction; + +import bean.OnlnSuggestionMsg; +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.EncryptUtil; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +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 + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) { + OnlnSuggestionMsg suggesstion = null; + // 状态码 + 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 suggesstionData = getJsonData(request); + String name = ""; + String content = ""; + try { + name = suggesstionData.getString("name"); + content = suggesstionData.getString("content"); + } catch (NumberFormatException e) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + respWrite(code, message, null, respWriter); + return; + } + suggesstion = new OnlnSuggestionMsg(name,content); + Connection conn = JDBCUtils.getConnectionDruid(); + int res = DaoFactory.getUserDaoInstance().onlnSuggestionMsg(conn, suggesstion); + switch (res) { + case 0: + CusLogger.saveUserLog(suggesstion + "评价成功"); + break; + case 1: + code = CodeMsg.existingPhnNum; + message = CodeMsg.existingPhnNumMsg; + CusLogger.saveInfo(name + message); + break; + default: + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(name + message); + } + JDBCUtils.close(conn, null, null); + respWrite(code, message, suggesstion, respWriter); + } +} diff --git a/src/main/java/servlet/residentManagement/GetHlpPeopleServlet.java b/src/main/java/servlet/residentManagement/GetHlpPeopleServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..0c1334080663b7aa1759589186b458aecc1abe65 --- /dev/null +++ b/src/main/java/servlet/residentManagement/GetHlpPeopleServlet.java @@ -0,0 +1,46 @@ +package servlet.residentManagement; + +import bean.Resident; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +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; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetHlpPeopleServlet", value = "/manage/getHlpPeople") +public class GetHlpPeopleServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List theFloatingPopulation = DaoFactory.getUserDaoInstance().getHlpPeople(conn); + message = "共查询到"+theFloatingPopulation.size()+"条记录"; + respWrite(code,message,theFloatingPopulation,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/residentManagement/GetOlderServlet.java b/src/main/java/servlet/residentManagement/GetOlderServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..ebb01d0ce72158183aec4e52e3946802ca3fc64d --- /dev/null +++ b/src/main/java/servlet/residentManagement/GetOlderServlet.java @@ -0,0 +1,46 @@ +package servlet.residentManagement; + +import bean.Resident; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +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; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetOlderServlet", value = "/manage/getOlder") +public class GetOlderServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List theFloatingPopulation = DaoFactory.getUserDaoInstance().getOlder(conn); + message = "共查询到"+theFloatingPopulation.size()+"条记录"; + respWrite(code,message,theFloatingPopulation,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/residentManagement/GetTheFloatingPopulationServlet.java b/src/main/java/servlet/residentManagement/GetTheFloatingPopulationServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..323e8f34d11b757e5756b09bd3c3960e115d2b35 --- /dev/null +++ b/src/main/java/servlet/residentManagement/GetTheFloatingPopulationServlet.java @@ -0,0 +1,45 @@ +package servlet.residentManagement; + +import bean.Resident; +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetTheFloatingPopulationServlet", value = "/manage/getTheFloatingPopulation") +public class GetTheFloatingPopulationServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List theFloatingPopulation = DaoFactory.getUserDaoInstance().getTheFloatingPopulation(conn); + message = "共查询到"+theFloatingPopulation.size()+"条记录"; + respWrite(code,message,theFloatingPopulation,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/residentManagement/GetTheUnemployedServlet.java b/src/main/java/servlet/residentManagement/GetTheUnemployedServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..df127656ee1fe9244d12c24a5846539900940caf --- /dev/null +++ b/src/main/java/servlet/residentManagement/GetTheUnemployedServlet.java @@ -0,0 +1,46 @@ +package servlet.residentManagement; + +import bean.Resident; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +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; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetTheUnemployedServlet", value = "/manage/getTheUnemployed") +public class GetTheUnemployedServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List theFloatingPopulation = DaoFactory.getUserDaoInstance().getTheUnemployed(conn); + message = "共查询到"+theFloatingPopulation.size()+"条记录"; + respWrite(code,message,theFloatingPopulation,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/residentManagement/GetWomensGroupsServlet.java b/src/main/java/servlet/residentManagement/GetWomensGroupsServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..e20f903d92344ad4eef1525771d429bbb613cbf2 --- /dev/null +++ b/src/main/java/servlet/residentManagement/GetWomensGroupsServlet.java @@ -0,0 +1,46 @@ +package servlet.residentManagement; + +import bean.Resident; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +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; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetWomensGroupsServlet", value = "/manage/getWomensGroups") +public class GetWomensGroupsServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List theFloatingPopulation = DaoFactory.getUserDaoInstance().getWomensGroups(conn); + message = "共查询到"+theFloatingPopulation.size()+"条记录"; + respWrite(code,message,theFloatingPopulation,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/socialServ/GetAPropertyCoServlet.java b/src/main/java/servlet/socialServ/GetAPropertyCoServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..8f431cdd2dabad70a30fab58ba2cdb39153596ee --- /dev/null +++ b/src/main/java/servlet/socialServ/GetAPropertyCoServlet.java @@ -0,0 +1,44 @@ +package servlet.socialServ; + +import bean.Merch; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetAPropertyCoServlet", value = "/serve/getAPropertyCo") +public class GetAPropertyCoServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List propertyCo = DaoFactory.getUserDaoInstance().getAPropertyCo(conn); + message = "共查询到"+propertyCo.size()+"条记录"; + respWrite(code,message,propertyCo,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/socialServ/GetRuralMerchantsServlet.java b/src/main/java/servlet/socialServ/GetRuralMerchantsServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..75943594a1c21bde082fc2624ec63cbad61a88f0 --- /dev/null +++ b/src/main/java/servlet/socialServ/GetRuralMerchantsServlet.java @@ -0,0 +1,45 @@ +package servlet.socialServ; + +import bean.Merch; +import bean.Resident; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetRuralMerchantsServlet", value = "/serve/getRuralMerchants") +public class GetRuralMerchantsServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List ruralMerchants = DaoFactory.getUserDaoInstance().getRuralMerchants(conn); + message = "共查询到"+ruralMerchants.size()+"条记录"; + respWrite(code,message,ruralMerchants,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/socialServ/GetSurroundingCompaniesServlet.java b/src/main/java/servlet/socialServ/GetSurroundingCompaniesServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..4a4ea9541ebeabce7f5e72e5eaa526bad40276e0 --- /dev/null +++ b/src/main/java/servlet/socialServ/GetSurroundingCompaniesServlet.java @@ -0,0 +1,44 @@ +package servlet.socialServ; + +import bean.Merch; +import factory.DaoFactory; +import log.CusLogger; +import utils.CodeMsg; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.util.List; + +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "GetSurroundingCompaniesServlet", value = "/serve/getSurroundingCompanies") +public class GetSurroundingCompaniesServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + Connection conn = JDBCUtils.getConnectionDruid(); + List surroundingCompanies = DaoFactory.getUserDaoInstance().getSurroundingCompanies(conn); + message = "共查询到"+surroundingCompanies.size()+"条记录"; + respWrite(code,message,surroundingCompanies,respWriter); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request,response); + } +} diff --git a/src/main/java/servlet/socialServ/RegisterAsAMerchServlet.java b/src/main/java/servlet/socialServ/RegisterAsAMerchServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..535771e600adaf71c7b715900f3f93ab82d8ca3d --- /dev/null +++ b/src/main/java/servlet/socialServ/RegisterAsAMerchServlet.java @@ -0,0 +1,96 @@ +package servlet.socialServ; + +import bean.Merch; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.JDBCUtils; + +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; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + + +/** + * @author zdy + */ +@WebServlet(name = "RegisterAsAMerchServlet", value = "/serve/registerAsAMerch") +public class RegisterAsAMerchServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Merch merch = null; + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + // 获取请求数据 + JSONObject registerData = getJsonData(request); + String name = ""; + String icoUrl = ""; + String introduction = ""; + String details = ""; + int type = 0; + try { + name = registerData.getString("name"); + icoUrl = registerData.getString("icoUrl"); + introduction = registerData.getString("introduction"); + details = registerData.getString("details"); + type = registerData.getInt("type"); + } catch (NumberFormatException e) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + respWrite(code, message, null, respWriter); + return; + } + // 数据验证 + if (type != 1 && type !=2 && type !=3 ) { + code = CodeMsg.wrongBusTyp; + message = CodeMsg.wrongBusTypMsg; + CusLogger.saveInfo(CodeMsg.wrongBusTypMsg); + respWrite(code, message, null, respWriter); + return; + } else { + merch = new Merch(name,icoUrl,introduction,details,type); + Connection conn = JDBCUtils.getConnectionDruid(); + int res = DaoFactory.getUserDaoInstance().registerAsAMerch(conn,merch); + switch (res) { + case 0: + CusLogger.saveUserLog(merch.toString() + "注册成功"); + break; + case 2: + code = CodeMsg.merchAlreadyExists; + message = CodeMsg.merchAlreadyExistsMsg; + CusLogger.saveInfo(name + message); + break; + default: + code = CodeMsg.registerAsMerchFailed; + message = CodeMsg.registerAsMerchFailedMsg; + CusLogger.saveInfo(merch.toString()+message); + } + JDBCUtils.close(conn,null,null); + } + respWrite(code,message,merch,respWriter); + } +} diff --git a/src/main/java/servlet/usrManagement/DeleteServlet.java b/src/main/java/servlet/usrManagement/DeleteServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..98eebddca8c23fb80cdfd932c58b55212f62bf97 --- /dev/null +++ b/src/main/java/servlet/usrManagement/DeleteServlet.java @@ -0,0 +1,80 @@ +package servlet.usrManagement; + +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.EncryptUtil; +import utils.JDBCUtils; + +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; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "DeleteServlet", value = "/user/delete") +public class DeleteServlet extends HttpServlet { + + @Override + protected void doDelete(HttpServletRequest request, HttpServletResponse response){ + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + JSONObject loginData = getJsonData(request); + String tel = loginData.getString("tel"); + String pwd = loginData.getString("pwd"); + if ("".equals(tel) || "".equals(pwd)) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + 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); + if(delete > 0) { + code = CodeMsg.success; + message = CodeMsg.successMsg; + respWrite(code, message, user, respWriter); + return; + } + code = CodeMsg.deleteFailed; + message = CodeMsg.deleteFailedMsg; + } + respWrite(code, message, null, respWriter); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doDelete(request, response); + } +} diff --git a/src/main/java/servlet/usrManagement/LoginServlet.java b/src/main/java/servlet/usrManagement/LoginServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..d56ec51d1dda92ccfe66fc0f357eb5acc37536ef --- /dev/null +++ b/src/main/java/servlet/usrManagement/LoginServlet.java @@ -0,0 +1,71 @@ +package servlet.usrManagement; + +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.EncryptUtil; +import utils.JDBCUtils; + +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; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "LoginServlet", value = "/user/login") +public class LoginServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 状态码 + String code = CodeMsg.success; + // 提示信息 + String message = CodeMsg.successMsg; + PrintWriter respWriter = null; + try { + respWriter = response.getWriter(); + } catch (IOException e) { + CusLogger.saveError(e.getMessage()); + return; + } + JSONObject loginData = getJsonData(request); + String tel = loginData.getString("tel"); + String pwd = loginData.getString("pwd"); + if ("".equals(tel) || "".equals(pwd)) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + 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 { + code = CodeMsg.success; + message = CodeMsg.successMsg; + respWrite(code,message,user,respWriter); + return; + } + respWrite(code,message,null,respWriter); + + } +} diff --git a/src/main/java/servlet/usrManagement/RegisterServlet.java b/src/main/java/servlet/usrManagement/RegisterServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..d3f3380c69dbc1f90234f19311b4bd3b5ed5dcdd --- /dev/null +++ b/src/main/java/servlet/usrManagement/RegisterServlet.java @@ -0,0 +1,116 @@ +package servlet.usrManagement; + +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.EncryptUtil; +import utils.JDBCUtils; + +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; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + +/** + * @author zdy + */ +@WebServlet(name = "RegisterServlet", value = "/user/register") +public class RegisterServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) { + User user = new User(); + // 状态码 + 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); + String tel = ""; + String name = ""; + String pwd = ""; + String gender = ""; + int age = 0; + int emplmtState = 1; + String profession = ""; + int incm = 0; + int whetherTheFloatingPopulation = 1; + String address = ""; + try { + tel = registerData.getString("tel"); + name = registerData.getString("name"); + pwd = registerData.getString("pwd"); + gender = registerData.getString("gender"); + age = registerData.getInt("age"); + emplmtState = registerData.getInt("emplmtState"); + profession = registerData.getString("profession"); + incm = registerData.getInt("incm"); + 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) { + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(message); + } else { + // 密码加密 + pwd = EncryptUtil.encrypt(pwd); + // user + user = new User(tel, name, pwd, gender, age, emplmtState, profession, incm,whetherTheFloatingPopulation,address); + Connection conn = JDBCUtils.getConnectionDruid(); + 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(code, message, user, respWriter); + } +} diff --git a/src/main/java/servlet/usrManagement/UpdateServlet.java b/src/main/java/servlet/usrManagement/UpdateServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..e11efff7a6165a05c0a6d72edeb937796f4ec832 --- /dev/null +++ b/src/main/java/servlet/usrManagement/UpdateServlet.java @@ -0,0 +1,108 @@ +package servlet.usrManagement; + +import bean.User; +import factory.DaoFactory; +import log.CusLogger; +import org.json.JSONObject; +import utils.CodeMsg; +import utils.EncryptUtil; +import utils.JDBCUtils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; + +import static utils.ServletUtils.getJsonData; +import static utils.ServletUtils.respWrite; + +@WebServlet(name = "UpdateServlet", value = "/user/update") +public class UpdateServlet extends HttpServlet { + @Override + protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req,resp); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + User user; + // 状态码 + 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); + String preTel = ""; + String tel = ""; + String name = ""; + String pwd = ""; + String gender = ""; + int age = 0; + int emplmtState = 1; + String profession = ""; + int incm = 0; + int whetherTheFloatingPopulation = 1; + String address = ""; + try { + preTel = registerData.getString("preTel"); + tel = registerData.getString("tel"); + name = registerData.getString("name"); + pwd = registerData.getString("pwd"); + gender = registerData.getString("gender"); + age = registerData.getInt("age"); + emplmtState = registerData.getInt("emplmtState"); + profession = registerData.getString("profession"); + incm = registerData.getInt("incm"); + whetherTheFloatingPopulation = registerData.getInt("whetherTheFloatingPopulation"); + address = registerData.getString("addresss"); + } catch (NumberFormatException e) { + code = CodeMsg.fieldIsMissing; + message = CodeMsg.fieldIsMissingMsg; + CusLogger.saveInfo(CodeMsg.fieldIsMissingMsg); + 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) { + code = CodeMsg.addFailed; + message = CodeMsg.addFailedMsg; + CusLogger.saveInfo(message); + } else { + // 密码加密 + pwd = EncryptUtil.encrypt(pwd); + // user + 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); + if (res == 0) { + code = CodeMsg.success; + message = CodeMsg.successMsg; + respWrite(code, message, user, respWriter); + JDBCUtils.close(conn, null, null); + return; + } else if (res == 2) { + code = CodeMsg.usrDoesNotExist; + message = CodeMsg.usrDoesNotExistMsg; + } + } + respWrite(code, message, null, respWriter); + } +} diff --git a/src/main/java/utils/CodeMsg.java b/src/main/java/utils/CodeMsg.java new file mode 100644 index 0000000000000000000000000000000000000000..5101c32b9b1d1c89c1f74f14bd98c00bf44a8c1a --- /dev/null +++ b/src/main/java/utils/CodeMsg.java @@ -0,0 +1,86 @@ +package utils; + +/** + * @author zdy + */ +public class CodeMsg { + public static String success = "200"; + public static String successMsg = "成功"; + /** + * 字段缺失 + */ + public static String fieldIsMissing = "2001"; + public static String fieldIsMissingMsg = "字段缺失"; + + /** + * map转为json数据失败 + */ + public static String toJsonFailed = "2002"; + public static String toJsonFailMsg = "转为json数据失败"; + + + /** + * 登录失败 + */ + public static String loginFailed = "2101"; + public static String loginFailMsg = "登陆失败"; + + /** + * 密码错误 + */ + public static String wrongPwd = "2103"; + public static String wrongPwdMsg = "密码错误"; + /** + * 用户不存在 + */ + public static String usrDoesNotExist = "2104"; + public static String usrDoesNotExistMsg = "用户不存在"; + + + /** + * 添加失败 + */ + public static String addFailed = "2201"; + public static String addFailedMsg = "添加失败"; + /** + * 手机号重复 + */ + public static String existingPhnNum = "2202"; + public static String existingPhnNumMsg = "此手机号已被注册"; + + + /** + * 更新失败 + */ + public static String updateFailed = "2301"; + public static String updateFailedMsg = "更新失败"; + + /** + * 登记为商户/企业/物业失败 + */ + public static String registerAsMerchFailed = "2401"; + public static String registerAsMerchFailedMsg = "登记为商户/企业/物业失败"; + /** + * 商户已存在 + */ + public static String merchAlreadyExists = "2402"; + public static String merchAlreadyExistsMsg = "商户已存在"; + /** + * 错误的商户类型 + */ + public static String wrongBusTyp = "2403"; + public static String wrongBusTypMsg = "错误的商户类型"; + + /** + * 非法的星数 + */ + public static String illegalNumOfStars = "2502"; + public static String illegalNumOfStarsMsg = "非法的星数"; + + /** + * 删除失败 + */ + public static String deleteFailed = "2601"; + public static String deleteFailedMsg = "删除失败"; + +} diff --git a/src/main/java/utils/EncryptUtil.java b/src/main/java/utils/EncryptUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..30b82e8d3d5331bf9269c849d7401eedf567a509 --- /dev/null +++ b/src/main/java/utils/EncryptUtil.java @@ -0,0 +1,176 @@ +package utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; + +/** + * 加密工具类 + *

+ * md5加密出来的长度是32位 + *

+ * sha加密出来的长度是40位 + * @author zdy + */ +public final class EncryptUtil { + + private static final String PASSWORD_CRYPT_KEY = "88444488"; + + private final static String DES = "DES"; + + /** + * 二次加密 先sha-1加密再用MD5加密 + * + * @param src + * @return + */ + public final static String md5AndSha(String src) { + return md5(sha(src)); + } + + /** + * 二次加密 先MD5加密再用sha-1加密 + * + * @param src + * @return + */ + public final static String shaAndMd5(String src) { + return sha(md5(src)); + } + + /** + * md5加密 + * + * @param src + * @return + */ + public final static String md5(String src) { + return encrypt(src, "md5"); + } + + /** + * sha-1加密 + * + * @param src + * @return + */ + public final static String sha(String src) { + return encrypt(src, "sha-1"); + } + + /** + * md5或者sha-1加密 + * + * @param src 要加密的内容 + * @param algorithmName 加密算法名称:md5或者sha-1,不区分大小写 + * @return + */ + private final static String encrypt(String src, String algorithmName) { + if (src == null || "".equals(src.trim())) { + throw new IllegalArgumentException("请输入要加密的内容"); + } + if (algorithmName == null || "".equals(algorithmName.trim())) { + algorithmName = "md5"; + } + String encryptText = null; + try { + MessageDigest m = MessageDigest.getInstance(algorithmName); + m.update(src.getBytes("UTF8")); + byte s[] = m.digest(); + // m.digest(src.getBytes("UTF8")); + return hex(s); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return encryptText; + } + + /** + * 密码加密 + * + * @param password + * @return + * @throws Exception + */ + public final static String encrypt(String password) { + try { + return byte2hex(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY.getBytes())); + } catch (Exception e) { + } + return null; + } + + /** + * 加密 + * + * @param src 数据源 + * @param key 密钥,长度必须是8的倍数 + * @return 返回加密后的数据 + * @throws Exception + */ + private static byte[] encrypt(byte[] src, byte[] key) throws Exception { + // DES算法要求有一个可信任的随机数源 + SecureRandom sr = new SecureRandom(); + // 从原始密匙数据创建DESKeySpec对象 + DESKeySpec dks = new DESKeySpec(key); + // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); + SecretKey securekey = keyFactory.generateSecret(dks); + // Cipher对象实际完成加密操作 + Cipher cipher = Cipher.getInstance(DES); + // 用密匙初始化Cipher对象 + cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); + // 现在,获取数据并加密正式执行加密操作 + return cipher.doFinal(src); + } + + /** + * 二行制转字符串 + * + * @param b + * @return + */ + private final static String byte2hex(byte[] b) { + String hs = ""; + String stmp = ""; + for (int n = 0; n < b.length; n++) { + stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); + if (stmp.length() == 1) { + hs = hs + "0" + stmp; + } else { + hs = hs + stmp; + } + + } + return hs.toUpperCase(); + } + + /** + * 返回十六进制字符串 + * + * @param arr + * @return + */ + private final static String hex(byte[] arr) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < arr.length; ++i) { + sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3)); + } + return sb.toString(); + } + + +// public static void main(String[] args) { +// String encrypt = EncryptUtil.encrypt("12345678"); +// System.out.println(encrypt); +// } + +} diff --git a/src/main/java/utils/JDBCUtils.java b/src/main/java/utils/JDBCUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..69c9bc0192e89e3f622eec062cad8d67ab8e5969 --- /dev/null +++ b/src/main/java/utils/JDBCUtils.java @@ -0,0 +1,54 @@ +package utils; + +import com.alibaba.druid.pool.DruidDataSourceFactory; + +import javax.sql.DataSource; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +/** + * @author zdy + */ +public class JDBCUtils { + /** + * 使用Druid连接池 + */ + private static DataSource source=null; + static { + try { + Properties pro=new Properties(); + InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); + pro.load(is); + //加载驱动 + source = DruidDataSourceFactory.createDataSource(pro); + }catch (Exception e) { + e.printStackTrace(); + } + } + + public static Connection getConnectionDruid(){ + Connection conn = null; + try { + conn = source.getConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + + public static void close(Connection conn, Statement stat, ResultSet rs){ + if(conn != null){ + try {conn.close();} catch (SQLException e) {e.printStackTrace();} + } + if(stat != null){ + try {stat.close();} catch (SQLException e) {e.printStackTrace();} + } + if(rs != null){ + try {rs.close();} catch (SQLException e) {e.printStackTrace();} + } + } +} diff --git a/src/main/java/utils/OnLineList.java b/src/main/java/utils/OnLineList.java new file mode 100644 index 0000000000000000000000000000000000000000..31b5d3e501f783c5d08db370952821196a3552af --- /dev/null +++ b/src/main/java/utils/OnLineList.java @@ -0,0 +1,7 @@ +package utils; + +import java.util.HashSet; + +public class OnLineList { + public static HashSet list = new HashSet<>(); +} diff --git a/src/main/java/utils/ServletUtils.java b/src/main/java/utils/ServletUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..09e7c4b24816170aaeb38f9ffb039f56bc306385 --- /dev/null +++ b/src/main/java/utils/ServletUtils.java @@ -0,0 +1,53 @@ +package utils; + +import bean.User; +import com.google.gson.Gson; +import log.CusLogger; +import org.json.JSONObject; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; + +public class ServletUtils { + public static void respWrite(String code, String message, Object data, PrintWriter respWriter) { + Map map = new HashMap(3); + map.put("code", code); + map.put("message", message); + map.put("data", data); + String respStr = ""; + try { + respStr = new Gson().toJson(map); + } catch (Exception e) { + CusLogger.saveError(CodeMsg.toJsonFailMsg); + } + if (respWriter != null) { + respWriter.print(respStr); + } else { + CusLogger.saveError("respWriter is null"); + } + } + + public static JSONObject getJsonData(HttpServletRequest request) { + JSONObject jsonData = null; + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ServletInputStream is = request.getInputStream(); + byte[] buffer = new byte[1024]; + + //先把字节流读取到buffer 然后再写入outputStream + while (is.read(buffer) > 0) { + outputStream.write(buffer); + } + outputStream.close(); + jsonData = new JSONObject(outputStream.toString("utf-8")); + } catch (Exception e) { + CusLogger.saveError(e.getMessage()); + return null; + } + return jsonData; + } +} diff --git a/src/main/resources/druid.properties b/src/main/resources/druid.properties new file mode 100644 index 0000000000000000000000000000000000000000..f1b9a9d6be8f577101d44fa27458c7d864b7e28c --- /dev/null +++ b/src/main/resources/druid.properties @@ -0,0 +1,9 @@ +url=jdbc:mysql://47.93.88.66:3306/test?rewriteBatchedStatements=true +username=root +password=123456 +driverClassName=com.mysql.cj.jdbc.Driver + +initialSize=10 +maxActive=20 +maxWait=1000 +filters=wall diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000000000000000000000000000000000000..29df08ab82f92e5cd6efc006dd44f3d87a5deba0 --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,47 @@ +#配置根Logger +#改代码表示输输出info级别以上的日志,文件分别输出,一个是file,一个是error +log4j.rootLogger=info,file,error + +#配置file日志信息输出目的地Appender +#定义名为file的输出端是每天产生一个日志文件 +log4j.appender.file=org.apache.log4j.DailyRollingFileAppender +#指定日志信息的最低输出级别位INFO,默认为DEBUG。 +log4j.appender.file.Threshold=INFO +#指定当前消息输出到jpm/log4j/log.log文件中 +log4j.appender.file.File=log/log.log +#指定按天来滚动日志文件 +log4j.appender.file.DatePattern=yyyy-MM-dd +#配置日志信息的格式(布局)Layout是可以灵活地指定布局模式 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +#格式化日志,Log4j采用类似C语言中的printf函数的打印格式格式化日志信息 +log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-ddHH:mm:ss}][%-5p][jpm-%c{1}-%M(%L)]-%m%n +#指定输出信息的编码 +log4j.appender.file.encoding=UTF-8 + +#配置error日志信息输出目的地Appender +#定义名为error的输出端是每天产生一个日志文件 +log4j.appender.error=org.apache.log4j.DailyRollingFileAppender +#指定日志信息的最低输出级别位ERROR,默认为DEBUG。 +log4j.appender.error.Threshold=ERROR +#指定当前消息输出到error.log文件中 +log4j.appender.error.File=log/error.log +#指定按月来滚动日志文件 +log4j.appender.error.DatePattern=yyyy-MM +#配置日志信息的格式(布局)Layout是可以灵活地指定布局模式 +log4j.appender.error.layout=org.apache.log4j.PatternLayout +#格式化日志,Log4j采用类似C语言中的printf函数的打印格式格式化日志信息 +log4j.appender.error.layout.ConversionPattern=[%d{yyyy-MM-ddHH:mm:ss}][%-5p][jpm-%c{1}-%M(%L)]-%m%n +#指定输出信息的编码 +#log4j.appender.error.encoding=UTF-8 + +#使某个功能的日志单独输出到指定的日志文件 +log4j.logger.saveUserLog=INFO,saveUserLog +#该配置就是让job的日志只输出到自己指定的日志文件中,表示Logger不会在父Logger的appender里输出,默认为true。 +log4j.additivity.saveUserLog=false +log4j.appender.saveUserLog=org.apache.log4j.DailyRollingFileAppender +log4j.appender.saveUserLog.File=log/saveUserLog.log +log4j.appender.saveUserLog.DatePattern=yyyy-MM-dd +log4j.appender.saveUserLog.Append=true +log4j.appender.saveUserLog.layout=org.apache.log4j.PatternLayout +log4j.appender.saveUserLog.layout.ConversionPattern=%m%n +log4j.appender.error.encoding=UTF-8 \ No newline at end of file diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..a942c54dc59b29774e6546277398ad7b23742227 --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + + characterFilter + filter.CharacterFilter + + + characterFilter + /* + + + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..77d31b3447de5d3e125c3ab6136aa06fe46ec289 --- /dev/null +++ b/web/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + bean.User: zdy + Date: 2022/6/8 + Time: 15:40 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + +