From 419b650652315f4d8b266694443d0f54c9d8597f Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Mon, 3 Jul 2023 21:18:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=B4=A1=E7=8C=AE=E5=80=BC?= =?UTF-8?q?=E5=92=8C=E6=90=9C=E7=B4=A2=E7=94=A8=E6=88=B7=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=9A=84=E2=80=9C=E6=9C=AA=E7=99=BB=E5=BD=95=E2=80=9D=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servlet/contributionValueSevlet.java | 30 +++++++++++++++---- .../servlet/selectUserLabelServlet.java | 10 +++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/com/hellogithub/servlet/contributionValueSevlet.java b/src/com/hellogithub/servlet/contributionValueSevlet.java index 3d424bc..063a688 100644 --- a/src/com/hellogithub/servlet/contributionValueSevlet.java +++ b/src/com/hellogithub/servlet/contributionValueSevlet.java @@ -1,8 +1,11 @@ package com.hellogithub.servlet; import com.alibaba.fastjson.JSONObject; +import com.hellogithub.dao.UserDao; +import com.hellogithub.entity.articleEntity; import com.hellogithub.entity.userEntity; import com.hellogithub.service.UserService; +import com.hellogithub.service.articleService; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; @@ -12,11 +15,14 @@ import jakarta.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; +import java.util.HashMap; +import java.util.List; @WebServlet("/contributionValue") public class contributionValueSevlet extends HttpServlet { - private UserService userService = new UserService(); + protected UserService userService = new UserService(); @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @@ -34,13 +40,27 @@ public class contributionValueSevlet extends HttpServlet { resp.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = resp.getWriter(); HttpSession session = req.getSession(); userEntity user = (userEntity)session.getAttribute("user"); - String userName = user.getUserName(); - int Value = userService.contributionValueReturn(userName); - writer.println(Value); + if(user == null){ + setResultError("用户暂未登录", writer); + }else{ + String userName = user.getUserName(); + int Value = userService.contributionValueReturn(userName); + writer.println(Value); + } + 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); + } } diff --git a/src/com/hellogithub/servlet/selectUserLabelServlet.java b/src/com/hellogithub/servlet/selectUserLabelServlet.java index bea0339..f7b8b87 100644 --- a/src/com/hellogithub/servlet/selectUserLabelServlet.java +++ b/src/com/hellogithub/servlet/selectUserLabelServlet.java @@ -35,9 +35,13 @@ public class selectUserLabelServlet extends HttpServlet { PrintWriter writer = resp.getWriter(); HttpSession session = req.getSession(); userEntity userEntity=(userEntity)session.getAttribute("user"); - String name = userEntity.getUserName(); - String jsonString = JSONObject.toJSONString(userService.selectLabel(name)); - writer.println(jsonString); + if(userEntity == null){ + setResultError("用户暂未登录", writer); + }else{ + String name = userEntity.getUserName(); + String jsonString = JSONObject.toJSONString(userService.selectLabel(name)); + writer.println(jsonString); + } // setResultOK("success",writer); writer.close();