From 9c4e65739648ddec66365ff0d6b31bf347d33cd3 Mon Sep 17 00:00:00 2001 From: Qing Date: Mon, 3 Jul 2023 10:09:54 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/hellogithub.sql | 32 +++++++++++++------ .../hellogithub/servlet/UserLoginServlet.java | 4 +-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/sql/hellogithub.sql b/sql/hellogithub.sql index eefde35..0a42aa6 100644 --- a/sql/hellogithub.sql +++ b/sql/hellogithub.sql @@ -11,7 +11,7 @@ Target Server Version : 80031 (8.0.31) File Encoding : 65001 - Date: 02/07/2023 22:50:04 + Date: 03/07/2023 10:09:21 */ SET NAMES utf8mb4; @@ -104,11 +104,12 @@ CREATE TABLE `comment` ( INDEX `projectId_comment`(`projectId` ASC) USING BTREE, CONSTRAINT `projectId_comment` FOREIGN KEY (`projectId`) REFERENCES `project` (`projectId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `userId_comment` FOREIGN KEY (`userId`) REFERENCES `project` (`userId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '评论表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '评论表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of comment -- ---------------------------- +INSERT INTO `comment` VALUES (1, 1, 1, 'hao好好', 1, '2023', 1); -- ---------------------------- -- Table structure for project @@ -134,14 +135,19 @@ CREATE TABLE `project` ( INDEX `categoryId_project`(`categoryId` ASC) USING BTREE, CONSTRAINT `categoryId_project` FOREIGN KEY (`categoryId`) REFERENCES `category` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `userId` FOREIGN KEY (`userId`) REFERENCES `user` (`userId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of project -- ---------------------------- -INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 111); -INSERT INTO `project` VALUES (2, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 222); -INSERT INTO `project` VALUES (3, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 3333); +INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 1); +INSERT INTO `project` VALUES (2, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 22); +INSERT INTO `project` VALUES (3, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 3, 1, '1111', 1, 333); +INSERT INTO `project` VALUES (4, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 4, 1, '1111', 1, 4444); +INSERT INTO `project` VALUES (5, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 5, 1, '1111', 1, 55555); +INSERT INTO `project` VALUES (6, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 6, 1, '1111', 1, 666666); +INSERT INTO `project` VALUES (7, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 7, 1, '1111', 1, 7777777); +INSERT INTO `project` VALUES (8, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 8, 1, '1111', 1, 88888888); -- ---------------------------- -- Table structure for project_category @@ -156,15 +162,20 @@ CREATE TABLE `project_category` ( INDEX `projectId`(`projectId` ASC) USING BTREE, CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `category` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `projectId` FOREIGN KEY (`projectId`) REFERENCES `project` (`projectId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目标签中间表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目标签中间表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of project_category -- ---------------------------- INSERT INTO `project_category` VALUES (1, 1, 1); -INSERT INTO `project_category` VALUES (2, 2, 1); +INSERT INTO `project_category` VALUES (2, 2, 2); INSERT INTO `project_category` VALUES (3, 3, 3); -INSERT INTO `project_category` VALUES (4, 1, 2); +INSERT INTO `project_category` VALUES (4, 4, 4); +INSERT INTO `project_category` VALUES (5, 5, 5); +INSERT INTO `project_category` VALUES (6, 6, 6); +INSERT INTO `project_category` VALUES (7, 7, 7); +INSERT INTO `project_category` VALUES (8, 8, 8); +INSERT INTO `project_category` VALUES (9, 2, 1); -- ---------------------------- -- Table structure for user @@ -185,6 +196,7 @@ CREATE TABLE `user` ( -- ---------------------------- -- Records of user -- ---------------------------- -INSERT INTO `user` VALUES (1, '666', '666', '2023-06-29 22:43:22', 1, 1, '666;777;33;测试;hhh;rrr;12e;', NULL); +INSERT INTO `user` VALUES (1, '666', '666', '2023-06-29 22:43:22', 1, 1, '11111;C++;C项目;github;', 1); +INSERT INTO `user` VALUES (2, '111', '111', '2023-06-29 22:43:22', 1, 1, '', 2); SET FOREIGN_KEY_CHECKS = 1; diff --git a/src/com/hellogithub/servlet/UserLoginServlet.java b/src/com/hellogithub/servlet/UserLoginServlet.java index 56d687e..5705d0a 100644 --- a/src/com/hellogithub/servlet/UserLoginServlet.java +++ b/src/com/hellogithub/servlet/UserLoginServlet.java @@ -69,9 +69,9 @@ public class UserLoginServlet extends HttpServlet { if ("true".equals(rememberPassword)) { // 如果有记住密码则 将密码保存在cookie中 Cookie userNameCookie = new Cookie("userName", userName); - Cookie userPwdCookie = new Cookie("userPwd", userPwd); +// Cookie userPwdCookie = new Cookie("userPwd", userPwd); resp.addCookie(userNameCookie); - resp.addCookie(userPwdCookie); +// resp.addCookie(userPwdCookie); } // 能够db中查询到对象 登录成功了 将用户数据存放在session中 session = req.getSession(); From df7d6207eb2565570201addde13e5ee4ed897700 Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 10:24:50 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 19 +++++++++ src/com/hellogithub/service/UserService.java | 2 + .../servlet/selectByInputServlet.java | 39 ++++++++++++------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index af634eb..024cd11 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -231,4 +231,23 @@ public class UserDao { } } } + + public int addLabel(String username,String str){ + String label; + userEntity userEntity; + int num; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select searchHistory from user where userName=?"; + userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),username); + label = userEntity.getSearchHistory()+str+";"; + sql="UPDATE user SET searchHistory = ? WHERE userName= ?"; + num = runner.update(conn,sql,label,username); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return num; + } } diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 9b440dd..c083619 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -57,4 +57,6 @@ public class UserService { public long queryUserCount(){return userDao.queryUserCount();} public int contributionValueReturn(String name){return userDao.contributionValueReturn(name);} + + public int addLabel(String username,String str){return userDao.addLabel(username,str);} } diff --git a/src/com/hellogithub/servlet/selectByInputServlet.java b/src/com/hellogithub/servlet/selectByInputServlet.java index 9b7b131..f8b8c0f 100644 --- a/src/com/hellogithub/servlet/selectByInputServlet.java +++ b/src/com/hellogithub/servlet/selectByInputServlet.java @@ -3,6 +3,7 @@ package com.hellogithub.servlet; import com.alibaba.fastjson.JSONObject; import com.hellogithub.entity.projectEntity; import com.hellogithub.entity.userEntity; +import com.hellogithub.service.UserService; import com.hellogithub.service.projectService; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletException; @@ -19,6 +20,7 @@ import java.util.List; @WebServlet("/selectByInput") public class selectByInputServlet extends HttpServlet { private com.hellogithub.service.projectService projectService=new projectService(); + private UserService userService = new UserService(); @Override public void init(ServletConfig config) throws ServletException { @@ -41,7 +43,7 @@ public class selectByInputServlet extends HttpServlet { resp.setHeader("Access-Control-Max-Age", "3600"); resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); resp.setContentType("application/json;charset=UTF-8"); - //返回最新期刊数字 + //添加搜索历史 PrintWriter writer = resp.getWriter(); String str = req.getParameter("xxxxx"); List projectEntityList = projectService.selectByInput(str); @@ -51,24 +53,35 @@ public class selectByInputServlet extends HttpServlet { userEntity=(userEntity)session.getAttribute("user"); if(userEntity == null) { - + if(projectEntityList.size() == 0 ){ + setResultError("查询结果为空",writer); + } + else{ + String jsonString = JSONObject.toJSONString(projectEntityList); + setResultOK("success",writer); + writer.println(jsonString); + writer.close(); + setResultOK("success",writer); + } } else{ + String username = userEntity.getUserName(); + int num = userService.addLabel(username,str); - + if(projectEntityList.size() == 0 ){ + setResultError("查询结果为空",writer); + } + else{ + String jsonString = JSONObject.toJSONString(projectEntityList); + setResultOK("success",writer); + writer.println(jsonString); + writer.close(); + setResultOK("success",writer); + } } - if(projectEntityList.size() == 0 ){ - setResultError("查询结果为空",writer); - } - else{ - String jsonString = JSONObject.toJSONString(projectEntityList); - setResultOK("success",writer); - writer.println(jsonString); - writer.close(); - setResultOK("success",writer); - } + } public void setResult(Integer code, String msg, PrintWriter writer) { From 68e451ed9e571799c84c0a795b242bf2d2e883c8 Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 10:33:52 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/projectDao.java | 17 +++++++++++++++++ src/com/hellogithub/service/projectService.java | 3 +++ .../servlet/selectProByIdServlet.java | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/src/com/hellogithub/dao/projectDao.java b/src/com/hellogithub/dao/projectDao.java index a237490..d35f93c 100644 --- a/src/com/hellogithub/dao/projectDao.java +++ b/src/com/hellogithub/dao/projectDao.java @@ -187,4 +187,21 @@ public class projectDao { } return projectEntity; } + + /** + * 根据项目ID增加观看量 + */ + public int addProjectLookcount(String id ) { + int num; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "UPDATE project SET lookcount = lookcount+1 WHERE projectId= ?"; + num = runner.update(conn, sql,id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } } diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index 135222b..5aa50b8 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -123,4 +123,7 @@ public class projectService { projectEntity.setCategoryName(categoryEntity.getCategoryName()); return projectEntity; } + public int addProjectLookcount(String id ){ + return projectDao.addProjectLookcount(id); + } } \ No newline at end of file diff --git a/src/com/hellogithub/servlet/selectProByIdServlet.java b/src/com/hellogithub/servlet/selectProByIdServlet.java index 0401666..64b89ed 100644 --- a/src/com/hellogithub/servlet/selectProByIdServlet.java +++ b/src/com/hellogithub/servlet/selectProByIdServlet.java @@ -39,9 +39,14 @@ public class selectProByIdServlet extends HttpServlet { resp.setContentType("application/json;charset=UTF-8"); //按照分类返回期刊 String id = req.getParameter("xxxxxxx"); + int num = projectService.addProjectLookcount(id); + PrintWriter writer = resp.getWriter(); String jsonString = JSONObject.toJSONString(projectService.selectProById(id)); writer.println(jsonString); + if(num == 0 ){ + setResult(200, "观看量增加失败", writer); + } writer.close(); } public void setResult(Integer code, String msg, PrintWriter writer) { From 428d030c944b4bc07b93f79347ea85b575225d3a Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 10:55:44 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/projectDao.java | 17 +++++++++++++++++ src/com/hellogithub/service/projectService.java | 3 +++ .../servlet/selectProByIdServlet.java | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/com/hellogithub/dao/projectDao.java b/src/com/hellogithub/dao/projectDao.java index d35f93c..586602a 100644 --- a/src/com/hellogithub/dao/projectDao.java +++ b/src/com/hellogithub/dao/projectDao.java @@ -1,5 +1,6 @@ package com.hellogithub.dao; +import com.hellogithub.entity.categoryEntity; import com.hellogithub.entity.projectEntity; import com.hellogithub.utils.JdbcUtils; import org.apache.commons.dbutils.QueryRunner; @@ -204,4 +205,20 @@ public class projectDao { } return num; } + /** + * 返回标签 + */ + public List retProjectLabel(String id ) { + List categoryEntityList ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select categoryName from category where categoryId in( select categoryId from project_category where projectId = ?)"; + categoryEntityList = runner.query(conn, sql,new BeanListHandler<>(categoryEntity.class),id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return categoryEntityList; + } } diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index 5aa50b8..171cf70 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -126,4 +126,7 @@ public class projectService { public int addProjectLookcount(String id ){ return projectDao.addProjectLookcount(id); } + public List retProjectLabel(String id ){ + return projectDao.retProjectLabel(id); + } } \ No newline at end of file diff --git a/src/com/hellogithub/servlet/selectProByIdServlet.java b/src/com/hellogithub/servlet/selectProByIdServlet.java index 64b89ed..ae47893 100644 --- a/src/com/hellogithub/servlet/selectProByIdServlet.java +++ b/src/com/hellogithub/servlet/selectProByIdServlet.java @@ -38,12 +38,14 @@ public class selectProByIdServlet extends HttpServlet { resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); resp.setContentType("application/json;charset=UTF-8"); //按照分类返回期刊 - String id = req.getParameter("xxxxxxx"); + String id = req.getParameter("id"); int num = projectService.addProjectLookcount(id); PrintWriter writer = resp.getWriter(); String jsonString = JSONObject.toJSONString(projectService.selectProById(id)); + String jsonString1 = JSONObject.toJSONString(projectService.retProjectLabel(id)); writer.println(jsonString); + writer.println(jsonString1); if(num == 0 ){ setResult(200, "观看量增加失败", writer); } From e0ca7c5e70d2a288a4fb0d02b97617a0eac343a8 Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 11:06:43 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/service/projectService.java | 14 ++++++++++++-- .../hellogithub/servlet/selectProByIdServlet.java | 12 ++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index 171cf70..4299687 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -5,7 +5,10 @@ import com.hellogithub.dao.categoryDao; import com.hellogithub.dao.projectDao; import com.hellogithub.entity.categoryEntity; import com.hellogithub.entity.projectEntity; + +import java.util.HashMap; import java.util.List; +import java.util.Map; public class projectService { @@ -126,7 +129,14 @@ public class projectService { public int addProjectLookcount(String id ){ return projectDao.addProjectLookcount(id); } - public List retProjectLabel(String id ){ - return projectDao.retProjectLabel(id); + public String retProjectLabel(String id ){ + Map dataMap = new HashMap<>(); + List categoryEntityList = projectDao.retProjectLabel(id); + for(int i=0;i Date: Mon, 3 Jul 2023 11:18:08 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/service/projectService.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index 4299687..64599b3 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -1,11 +1,14 @@ package com.hellogithub.service; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.hellogithub.dao.categoryDao; import com.hellogithub.dao.projectDao; import com.hellogithub.entity.categoryEntity; import com.hellogithub.entity.projectEntity; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -130,13 +133,16 @@ public class projectService { return projectDao.addProjectLookcount(id); } public String retProjectLabel(String id ){ - Map dataMap = new HashMap<>(); + Map> dataMap = new HashMap<>(); + List strs=new ArrayList<>(); List categoryEntityList = projectDao.retProjectLabel(id); for(int i=0;i Date: Mon, 3 Jul 2023 11:26:41 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0sql=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 16 ++++++++-------- src/com/hellogithub/dao/articleDao.java | 4 ++-- src/com/hellogithub/dao/categoryDao.java | 4 ++-- src/com/hellogithub/dao/projectDao.java | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 024cd11..4267e3c 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -92,7 +92,7 @@ public class UserDao { Connection connection = null; try { connection = JdbcUtils.getConnection(); - String loginSql = "select * from user where userName=?"; + String loginSql = "select * from user where userName=? and isValid = 1"; preparedStatement = connection.prepareStatement(loginSql); preparedStatement.setString(1, userName); resultSet = preparedStatement.executeQuery(); @@ -123,7 +123,7 @@ public class UserDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select searchHistory from user where userName=?"; + String sql = "select searchHistory from user where userName=? and isValid = 1"; userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),name); label = userEntity.getSearchHistory().replace(str+";",""); sql="UPDATE user SET searchHistory = ? WHERE userName= ?"; @@ -144,7 +144,7 @@ public class UserDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select searchHistory from user where userName=?"; + String sql = "select searchHistory from user where userName=? and isValid = 1"; userEntity1 = runner.query(conn, sql, new BeanHandler<>(userEntity.class),name); label=userEntity1.getSearchHistory(); } catch (SQLException e) { @@ -164,7 +164,7 @@ public class UserDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="UPDATE user SET searchHistory = ? WHERE userName= ?"; + String sql="UPDATE user SET searchHistory = ? WHERE userName= ? and isValid = 1"; num = runner.update(conn,sql,label,name); } catch (SQLException e) { throw new RuntimeException(e); @@ -183,7 +183,7 @@ public class UserDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="select count(*) as num from user"; + String sql="select count(*) as num from user where isValid = 1"; userEntity = runner.query(conn,sql,new BeanHandler<>(userEntity.class)); num = userEntity.getNum(); } catch (SQLException e) { @@ -201,7 +201,7 @@ public class UserDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `user` t1,article t2 WHERE t1.userId = t2.userId AND t1.userId = ?"; + String sql = "SELECT * FROM `user` t1,article t2 WHERE t1.userId = t2.userId AND t1.userId = ? and isValid = 1"; int Value = 0; try{ conn = JdbcUtils.getConnection(); @@ -239,10 +239,10 @@ public class UserDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select searchHistory from user where userName=?"; + String sql = "select searchHistory from user where userName=? and isValid = 1"; userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),username); label = userEntity.getSearchHistory()+str+";"; - sql="UPDATE user SET searchHistory = ? WHERE userName= ?"; + sql="UPDATE user SET searchHistory = ? WHERE userName= ? and isValid = 1"; num = runner.update(conn,sql,label,username); } catch (SQLException e) { throw new RuntimeException(e); diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java index 3629a4c..401ad1e 100644 --- a/src/com/hellogithub/dao/articleDao.java +++ b/src/com/hellogithub/dao/articleDao.java @@ -24,7 +24,7 @@ public class articleDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `article` ORDER BY publishTime DESC;"; + String sql = "SELECT * FROM `article` where isValid = 1 ORDER BY publishTime DESC;"; ArrayList articleEntities = new ArrayList(); try { conn = JdbcUtils.getConnection(); @@ -62,7 +62,7 @@ public class articleDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `article` ORDER BY readCount DESC"; + String sql = "SELECT * FROM `article` where isValid = 1 ORDER BY readCount DESC"; ArrayList articleEntities = new ArrayList(); try { conn = JdbcUtils.getConnection(); diff --git a/src/com/hellogithub/dao/categoryDao.java b/src/com/hellogithub/dao/categoryDao.java index 192960b..27b8ef6 100644 --- a/src/com/hellogithub/dao/categoryDao.java +++ b/src/com/hellogithub/dao/categoryDao.java @@ -19,7 +19,7 @@ public class categoryDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from category "; + String sql = "select * from category where isValid = 1"; categoryEntity = runner.query(conn, sql, new BeanListHandler<>(categoryEntity.class)); conn.close(); } catch (SQLException e) { @@ -33,7 +33,7 @@ public class categoryDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from category where categoryId=?"; + String sql = "select * from category where categoryId=? and isValid = 1"; categoryEntity = runner.query(conn, sql, new BeanHandler<>(categoryEntity.class),id); conn.close(); } catch (SQLException e) { diff --git a/src/com/hellogithub/dao/projectDao.java b/src/com/hellogithub/dao/projectDao.java index 586602a..74bde94 100644 --- a/src/com/hellogithub/dao/projectDao.java +++ b/src/com/hellogithub/dao/projectDao.java @@ -213,7 +213,7 @@ public class projectDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select categoryName from category where categoryId in( select categoryId from project_category where projectId = ?)"; + String sql = "select categoryName from category where isValid = 1 and categoryId in( select categoryId from project_category where projectId = ? and isValid = 1)"; categoryEntityList = runner.query(conn, sql,new BeanListHandler<>(categoryEntity.class),id); conn.close(); } catch (SQLException e) { From 87abd49bc4dc008a41430fdeb876eb01273ec5d1 Mon Sep 17 00:00:00 2001 From: Qing Date: Mon, 3 Jul 2023 11:29:00 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/hellogithub.sql | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sql/hellogithub.sql b/sql/hellogithub.sql index 0a42aa6..8fc0e55 100644 --- a/sql/hellogithub.sql +++ b/sql/hellogithub.sql @@ -11,7 +11,7 @@ Target Server Version : 80031 (8.0.31) File Encoding : 65001 - Date: 03/07/2023 10:09:21 + Date: 03/07/2023 11:28:18 */ SET NAMES utf8mb4; @@ -99,6 +99,7 @@ CREATE TABLE `comment` ( `isUsed` int NULL DEFAULT NULL COMMENT '是否使用过', `commentTime` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '评论时间', `isValid` int NULL DEFAULT NULL COMMENT '是否有效', + `star` int NULL DEFAULT NULL COMMENT '评分', PRIMARY KEY (`commentId`) USING BTREE, INDEX `userId_comment`(`userId` ASC) USING BTREE, INDEX `projectId_comment`(`projectId` ASC) USING BTREE, @@ -109,7 +110,7 @@ CREATE TABLE `comment` ( -- ---------------------------- -- Records of comment -- ---------------------------- -INSERT INTO `comment` VALUES (1, 1, 1, 'hao好好', 1, '2023', 1); +INSERT INTO `comment` VALUES (1, 1, 1, 'hao好好', 1, '2023', 1, NULL); -- ---------------------------- -- Table structure for project @@ -140,8 +141,8 @@ CREATE TABLE `project` ( -- ---------------------------- -- Records of project -- ---------------------------- -INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 1); -INSERT INTO `project` VALUES (2, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 22); +INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 15); +INSERT INTO `project` VALUES (2, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 23); INSERT INTO `project` VALUES (3, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 3, 1, '1111', 1, 333); INSERT INTO `project` VALUES (4, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 4, 1, '1111', 1, 4444); INSERT INTO `project` VALUES (5, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 5, 1, '1111', 1, 55555); From c5dab5c31620fad6ee1588579dd9425880a1eb2f Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 12:12:42 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=EF=BC=8C=E4=BF=AE=E6=94=B9userEntity?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 17 +++++ src/com/hellogithub/dao/commentDao.java | 29 +++++++++ src/com/hellogithub/entity/commentEntity.java | 17 +++-- src/com/hellogithub/entity/userEntity.java | 8 +-- .../hellogithub/service/commentService.java | 39 ++++++++++++ .../servlet/selectCommentByProServlet.java | 63 +++++++++++++++++++ 6 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 src/com/hellogithub/dao/commentDao.java create mode 100644 src/com/hellogithub/service/commentService.java create mode 100644 src/com/hellogithub/servlet/selectCommentByProServlet.java diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 4267e3c..20f05cc 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -250,4 +250,21 @@ public class UserDao { return num; } + + public String selectNameById(int id) + { + String label; + userEntity userEntity; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from user where userId=? and isValid = 1"; + userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),id); + label = userEntity.getUserName(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return label; + } } diff --git a/src/com/hellogithub/dao/commentDao.java b/src/com/hellogithub/dao/commentDao.java new file mode 100644 index 0000000..a0750f3 --- /dev/null +++ b/src/com/hellogithub/dao/commentDao.java @@ -0,0 +1,29 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.categoryEntity; +import com.hellogithub.entity.commentEntity; +import com.hellogithub.utils.JdbcUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +public class commentDao { + + //显示评论 + public List selectByProjectId(String id){ + List commentEntityList ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from comment where projectId = ? and isValid = 1"; + commentEntityList = runner.query(conn, sql,new BeanListHandler<>(commentEntity.class),id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return commentEntityList; + } +} diff --git a/src/com/hellogithub/entity/commentEntity.java b/src/com/hellogithub/entity/commentEntity.java index d247946..6e08384 100644 --- a/src/com/hellogithub/entity/commentEntity.java +++ b/src/com/hellogithub/entity/commentEntity.java @@ -1,6 +1,5 @@ package com.hellogithub.entity; -import java.util.Date; public class commentEntity { int commentId; @@ -8,7 +7,7 @@ public class commentEntity { int projectId; String content; int isUsed; - Date commentTime; + String commentTime; int isValid; public int getCommentId() { @@ -51,11 +50,11 @@ public class commentEntity { this.isUsed = isUsed; } - public Date getCommentTime() { + public String getCommentTime() { return commentTime; } - public void setCommentTime(Date commentTime) { + public void setCommentTime(String commentTime) { this.commentTime = commentTime; } @@ -66,4 +65,14 @@ public class commentEntity { public void setIsValid(int isValid) { this.isValid = isValid; } + + int star; + + public int getStar() { + return star; + } + + public void setStar(int star) { + this.star = star; + } } diff --git a/src/com/hellogithub/entity/userEntity.java b/src/com/hellogithub/entity/userEntity.java index 823af4a..2d2c9e3 100644 --- a/src/com/hellogithub/entity/userEntity.java +++ b/src/com/hellogithub/entity/userEntity.java @@ -22,7 +22,7 @@ public class userEntity { private String userName; private String userPassword; - private Date creatTime; + private String creatTime; private Integer isValid; @@ -41,7 +41,7 @@ public class userEntity { this.userPassword = userPassword; } - public userEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { + public userEntity(Integer userId, String userName, String userPassword, String creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { this.userId = userId; this.userName = userName; this.userPassword = userPassword; @@ -76,11 +76,11 @@ public class userEntity { this.userPassword = userPassword; } - public Date getCreatTime() { + public String getCreatTime() { return creatTime; } - public void setCreatTime(Date creatTime) { + public void setCreatTime(String creatTime) { this.creatTime = creatTime; } diff --git a/src/com/hellogithub/service/commentService.java b/src/com/hellogithub/service/commentService.java new file mode 100644 index 0000000..b7a439a --- /dev/null +++ b/src/com/hellogithub/service/commentService.java @@ -0,0 +1,39 @@ +package com.hellogithub.service; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.dao.UserDao; +import com.hellogithub.dao.commentDao; +import com.hellogithub.entity.commentEntity; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class commentService { + private commentDao commentDao =new commentDao(); + private UserDao userDao = new UserDao(); + //显示所有评论 + public String selectByProjectId(String id){ + List commentEntityList; + Map dataMap = new HashMap<>(); + Map> dataMap2 = new HashMap<>(); + commentEntityList = commentDao.selectByProjectId(id); + for(int i=0;i(); + int userid = commentEntity.getUserId(); + String username = userDao.selectNameById(userid); + String time = commentEntity.getCommentTime(); + String content = commentEntity.getContent(); + int star = commentEntity.getStar(); + dataMap.put("username",username); + dataMap.put("time",time); + dataMap.put("content",content); + dataMap.put("star",String.valueOf(star)); + dataMap2.put(String.valueOf(i),dataMap); + } + return JSONArray.toJSONString(dataMap2); + } +} diff --git a/src/com/hellogithub/servlet/selectCommentByProServlet.java b/src/com/hellogithub/servlet/selectCommentByProServlet.java new file mode 100644 index 0000000..0d9f1a2 --- /dev/null +++ b/src/com/hellogithub/servlet/selectCommentByProServlet.java @@ -0,0 +1,63 @@ +package com.hellogithub.servlet; + +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.service.commentService; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; + +@WebServlet("/selectCommentByPro") +public class selectCommentByProServlet extends HttpServlet { + private commentService commentService = new commentService(); + @Override + public void init(ServletConfig config) throws ServletException { + + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding("UTF-8"); + resp.setContentType("text/html; charset=utf-8"); + // 设置响应头允许ajax跨域访问 + String curOrigin = req.getHeader("Origin"); + resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin); + resp.setHeader("Access-Control-Allow-Credentials", "true"); + resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); + resp.setHeader("Access-Control-Max-Age", "3600"); + resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); + resp.setContentType("application/json;charset=UTF-8"); + //按照分类检索 + String id = req.getParameter("id"); + PrintWriter writer = resp.getWriter(); + writer.println(commentService.selectByProjectId(id)); + writer.close(); + } + public void setResult(Integer code, String msg, PrintWriter writer) { + HashMap result = new HashMap<>(); + result.put("code", code); + result.put("msg", msg); + String jsonString = JSONObject.toJSONString(result); + writer.println(jsonString); + writer.close(); + } + + public void setResultError(String msg, PrintWriter writer) { + setResult(500, msg, writer); + } + + public void setResultOK(String msg, PrintWriter writer) { + setResult(200, msg, writer); + } +}