From f4a34a90bc12d7507e17d14c52a3f91e32704b5c Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Mon, 7 Oct 2024 17:00:31 +0900 Subject: [PATCH 01/27] =?UTF-8?q?feat:=20servlet=20jsp=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=20=EC=98=88=EC=8B=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jwp/controller/CreateUserController.java | 25 +++++++++++++++++++ .../jwp/controller/ListUserController.java | 21 ++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/java/jwp/controller/CreateUserController.java create mode 100644 src/main/java/jwp/controller/ListUserController.java diff --git a/src/main/java/jwp/controller/CreateUserController.java b/src/main/java/jwp/controller/CreateUserController.java new file mode 100644 index 00000000..8589b184 --- /dev/null +++ b/src/main/java/jwp/controller/CreateUserController.java @@ -0,0 +1,25 @@ +package jwp.controller; + +import core.db.MemoryUserRepository; +import jwp.model.User; + +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; + +@WebServlet("/user/signup") +public class CreateUserController extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + User user = new User(req.getParameter("userId"), + req.getParameter("password"), + req.getParameter("name"), + req.getParameter("email")); + MemoryUserRepository.getInstance().addUser(user); + System.out.println("??"); + resp.sendRedirect("/user/list"); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/controller/ListUserController.java b/src/main/java/jwp/controller/ListUserController.java new file mode 100644 index 00000000..aeb3f305 --- /dev/null +++ b/src/main/java/jwp/controller/ListUserController.java @@ -0,0 +1,21 @@ +package jwp.controller; + +import core.db.MemoryUserRepository; + +import javax.servlet.RequestDispatcher; +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; + +@WebServlet("/user/list") +public class ListUserController extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setAttribute("users", MemoryUserRepository.getInstance().findAll()); + RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); + rd.forward(req, resp); + } +} \ No newline at end of file From 33f5315894b97bffb7931fa976b4056157894504 Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Mon, 7 Oct 2024 17:20:36 +0900 Subject: [PATCH 02/27] =?UTF-8?q?feat:=20jsp=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?,=20HomeController=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jwp/controller/CreateUserController.java | 2 +- .../java/jwp/controller/HomeController.java | 18 +++ webapp/{index.html => home.jsp} | 62 +-------- webapp/include/header.jspf | 7 + webapp/include/navigation.jspf | 42 ++++++ webapp/qna/form.html | 80 ------------ webapp/qna/form.jsp | 41 ++++++ webapp/qna/{show.html => show.jsp} | 58 +-------- webapp/user/form.html | 85 ------------ webapp/user/form.jsp | 42 ++++++ webapp/user/list.html | 95 -------------- webapp/user/list.jsp | 122 ++++++------------ webapp/user/login.html | 74 ----------- webapp/user/login.jsp | 31 +++++ webapp/user/login_failed.html | 77 ----------- webapp/user/login_failed.jsp | 33 +++++ 16 files changed, 266 insertions(+), 603 deletions(-) create mode 100644 src/main/java/jwp/controller/HomeController.java rename webapp/{index.html => home.jsp} (50%) create mode 100644 webapp/include/header.jspf create mode 100644 webapp/include/navigation.jspf delete mode 100644 webapp/qna/form.html create mode 100644 webapp/qna/form.jsp rename webapp/qna/{show.html => show.jsp} (73%) delete mode 100644 webapp/user/form.html create mode 100644 webapp/user/form.jsp delete mode 100644 webapp/user/list.html delete mode 100644 webapp/user/login.html create mode 100644 webapp/user/login.jsp delete mode 100644 webapp/user/login_failed.html create mode 100644 webapp/user/login_failed.jsp diff --git a/src/main/java/jwp/controller/CreateUserController.java b/src/main/java/jwp/controller/CreateUserController.java index 8589b184..70f85f38 100644 --- a/src/main/java/jwp/controller/CreateUserController.java +++ b/src/main/java/jwp/controller/CreateUserController.java @@ -19,7 +19,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S req.getParameter("name"), req.getParameter("email")); MemoryUserRepository.getInstance().addUser(user); - System.out.println("??"); + System.out.println("user 회원가입 완료"); resp.sendRedirect("/user/list"); } } \ No newline at end of file diff --git a/src/main/java/jwp/controller/HomeController.java b/src/main/java/jwp/controller/HomeController.java new file mode 100644 index 00000000..8c99a400 --- /dev/null +++ b/src/main/java/jwp/controller/HomeController.java @@ -0,0 +1,18 @@ +package jwp.controller; + +import javax.servlet.RequestDispatcher; +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; + +@WebServlet("/") +public class HomeController extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + RequestDispatcher rd = req.getRequestDispatcher("/home.jsp"); + rd.forward(req, resp); + } +} diff --git a/webapp/index.html b/webapp/home.jsp similarity index 50% rename from webapp/index.html rename to webapp/home.jsp index b5534c56..ac8aec83 100644 --- a/webapp/index.html +++ b/webapp/home.jsp @@ -1,59 +1,11 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + - - - - KUIT - - - +<%@ include file="/include/header.jspf" %> - - +<%@ include file="/include/navigation.jspf" %>

Q&A

@@ -63,7 +15,7 @@

Q&A

- 객체지향을 가장 잘 다룬 책이 뭐가 있나요? + 객체지향을 가장 잘 다룬 책이 뭐가 있나요?
@@ -82,7 +34,7 @@

Q&A

- 객체지향에서 가장 중요하다고 생각하는 것이 무엇인가요? + 객체지향에서 가장 중요하다고 생각하는 것이 무엇인가요?
diff --git a/webapp/include/header.jspf b/webapp/include/header.jspf new file mode 100644 index 00000000..6dae8486 --- /dev/null +++ b/webapp/include/header.jspf @@ -0,0 +1,7 @@ + + + + KUIT + + + \ No newline at end of file diff --git a/webapp/include/navigation.jspf b/webapp/include/navigation.jspf new file mode 100644 index 00000000..f3f5a282 --- /dev/null +++ b/webapp/include/navigation.jspf @@ -0,0 +1,42 @@ + + \ No newline at end of file diff --git a/webapp/qna/form.html b/webapp/qna/form.html deleted file mode 100644 index db01fcde..00000000 --- a/webapp/qna/form.html +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - KUIT - - - - - - - -
-
-
-
-
- - -
-
- - -
-
- - -
- -
- -
-
-
- - - - - \ No newline at end of file diff --git a/webapp/qna/form.jsp b/webapp/qna/form.jsp new file mode 100644 index 00000000..c44ab1b0 --- /dev/null +++ b/webapp/qna/form.jsp @@ -0,0 +1,41 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +<%@ include file="/include/header.jspf"%> + +<%@ include file="/include/navigation.jspf" %> + +
+ +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+
+
+ + + + + \ No newline at end of file diff --git a/webapp/qna/show.html b/webapp/qna/show.jsp similarity index 73% rename from webapp/qna/show.html rename to webapp/qna/show.jsp index 799200ef..7e897691 100644 --- a/webapp/qna/show.html +++ b/webapp/qna/show.jsp @@ -1,55 +1,11 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + - - - - KUIT - - - - - - +<%@ include file="/include/header.jspf"%> + +<%@ include file="/include/navigation.jspf" %>
@@ -87,7 +43,7 @@

객체지향을 가장 잘 다룬 책이 뭐가 있나요?
  • - 목록 + 목록
  • diff --git a/webapp/user/form.html b/webapp/user/form.html deleted file mode 100644 index 038f2caa..00000000 --- a/webapp/user/form.html +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - KUIT - - - - - - - -
    - -
    - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    -
    - -
    -
    -
    - - - - - \ No newline at end of file diff --git a/webapp/user/form.jsp b/webapp/user/form.jsp new file mode 100644 index 00000000..4b7817e1 --- /dev/null +++ b/webapp/user/form.jsp @@ -0,0 +1,42 @@ + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +<%@ include file="/include/header.jspf"%> + +<%@ include file="/include/navigation.jspf" %> + +
    + +
    + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    +
    + +
    +
    +
    + + + + + diff --git a/webapp/user/list.html b/webapp/user/list.html deleted file mode 100644 index 22ad9864..00000000 --- a/webapp/user/list.html +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - KUIT - - - - - - \ No newline at end of file diff --git a/webapp/user/login_failed.jsp b/webapp/user/loginFailed.jsp similarity index 100% rename from webapp/user/login_failed.jsp rename to webapp/user/loginFailed.jsp From 662a3b30a09389613038833689acfb76cf3f4306 Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Mon, 7 Oct 2024 19:41:00 +0900 Subject: [PATCH 05/27] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=B4=EC=95=88=20=EA=B0=95=ED=99=94=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jwp/controller/ListUserController.java | 13 +++++++++--- .../controller/UpdateUserFormController.java | 15 ++++++------- src/main/java/jwp/util/UserSessionUtils.java | 21 +++++++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 src/main/java/jwp/util/UserSessionUtils.java diff --git a/src/main/java/jwp/controller/ListUserController.java b/src/main/java/jwp/controller/ListUserController.java index aeb3f305..c05d4198 100644 --- a/src/main/java/jwp/controller/ListUserController.java +++ b/src/main/java/jwp/controller/ListUserController.java @@ -1,6 +1,7 @@ package jwp.controller; import core.db.MemoryUserRepository; +import jwp.util.UserSessionUtils; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -8,14 +9,20 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/user/list") public class ListUserController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - req.setAttribute("users", MemoryUserRepository.getInstance().findAll()); - RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); - rd.forward(req, resp); + HttpSession session = req.getSession(); + if (UserSessionUtils.isLogined(session)) { + req.setAttribute("users", MemoryUserRepository.getInstance().findAll()); + RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); + rd.forward(req, resp); + return; + } + resp.sendRedirect("/user/login.jsp"); } } \ No newline at end of file diff --git a/src/main/java/jwp/controller/UpdateUserFormController.java b/src/main/java/jwp/controller/UpdateUserFormController.java index 82359b54..89656b14 100644 --- a/src/main/java/jwp/controller/UpdateUserFormController.java +++ b/src/main/java/jwp/controller/UpdateUserFormController.java @@ -11,18 +11,19 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; -@WebServlet +@WebServlet("/user/updateForm") public class UpdateUserFormController extends HttpServlet { + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userId = req.getParameter("userId"); User user = MemoryUserRepository.getInstance().findUserById(userId); - if (user == null) { - resp.sendRedirect("/"); + if (user != null) { + req.setAttribute("user", user); + RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); + rd.forward(req, resp); return; } - req.setAttribute("user", user); - RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); - rd.forward(req, resp); + resp.sendRedirect("/"); } -} +} \ No newline at end of file diff --git a/src/main/java/jwp/util/UserSessionUtils.java b/src/main/java/jwp/util/UserSessionUtils.java new file mode 100644 index 00000000..1e7f3c28 --- /dev/null +++ b/src/main/java/jwp/util/UserSessionUtils.java @@ -0,0 +1,21 @@ +package jwp.util; + +import jwp.model.User; + +import javax.servlet.http.HttpSession; + +public class UserSessionUtils { + public static final String USER_SESSION_KEY = "user"; + + public static User getUserFromSession(HttpSession session){ + Object user = session.getAttribute(USER_SESSION_KEY); + if(user == null){ + return null; + } + return (User) user; + } + + public static boolean isLogined(HttpSession session){ + return getUserFromSession(session) != null; + } +} From 2906265598d78c4454f569b16ee8c60de9252666 Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Mon, 7 Oct 2024 19:49:39 +0900 Subject: [PATCH 06/27] =?UTF-8?q?feat:=20=EA=B0=9C=EC=9D=B8=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=88=98=EC=A0=95=20=EB=B3=B4=EC=95=88=20=EA=B0=95?= =?UTF-8?q?=ED=99=94=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UpdateUserFormController.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/jwp/controller/UpdateUserFormController.java b/src/main/java/jwp/controller/UpdateUserFormController.java index 89656b14..7fecdca2 100644 --- a/src/main/java/jwp/controller/UpdateUserFormController.java +++ b/src/main/java/jwp/controller/UpdateUserFormController.java @@ -9,6 +9,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/user/updateForm") @@ -16,13 +17,18 @@ public class UpdateUserFormController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String userId = req.getParameter("userId"); + String userId = req.getParameter("userId"); // 수정되는 user User user = MemoryUserRepository.getInstance().findUserById(userId); - if (user != null) { - req.setAttribute("user", user); - RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); - rd.forward(req, resp); - return; + + HttpSession session = req.getSession(); // 수정하는 user + Object value = session.getAttribute("user"); + + if (user != null && value != null) { + if (user.equals(value)){ // 수정되는 user와 수정하는 user가 동일한 경우 + RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); + rd.forward(req, resp); + return; + } } resp.sendRedirect("/"); } From f200d11cd3857daaec9697cc1262dd6f55a6b324 Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Mon, 7 Oct 2024 20:40:23 +0900 Subject: [PATCH 07/27] =?UTF-8?q?feat:=20mvc=20=ED=94=84=EB=A0=88=EC=9E=84?= =?UTF-8?q?=EC=9B=8C=ED=81=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/core/mvc/Controller.java | 8 ++++ src/main/java/core/mvc/DispatcherServlet.java | 42 +++++++++++++++++++ src/main/java/core/mvc/ForwardController.java | 21 ++++++++++ src/main/java/core/mvc/RequestMapping.java | 37 ++++++++++++++++ .../jwp/controller/CreateUserController.java | 13 +++--- .../java/jwp/controller/HomeController.java | 16 +++---- .../jwp/controller/ListUserController.java | 18 +++----- .../java/jwp/controller/LoginController.java | 17 +++----- .../java/jwp/controller/LogoutController.java | 13 +++--- .../jwp/controller/UpdateUserController.java | 12 ++---- .../controller/UpdateUserFormController.java | 19 +++------ webapp/home.jsp | 6 +-- webapp/include/navigation.jspf | 6 +-- webapp/qna/show.jsp | 2 +- 14 files changed, 153 insertions(+), 77 deletions(-) create mode 100644 src/main/java/core/mvc/Controller.java create mode 100644 src/main/java/core/mvc/DispatcherServlet.java create mode 100644 src/main/java/core/mvc/ForwardController.java create mode 100644 src/main/java/core/mvc/RequestMapping.java diff --git a/src/main/java/core/mvc/Controller.java b/src/main/java/core/mvc/Controller.java new file mode 100644 index 00000000..df7b7f2e --- /dev/null +++ b/src/main/java/core/mvc/Controller.java @@ -0,0 +1,8 @@ +package core.mvc; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public interface Controller { + String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception; +} diff --git a/src/main/java/core/mvc/DispatcherServlet.java b/src/main/java/core/mvc/DispatcherServlet.java new file mode 100644 index 00000000..9aa2913a --- /dev/null +++ b/src/main/java/core/mvc/DispatcherServlet.java @@ -0,0 +1,42 @@ +package core.mvc; + +import javax.servlet.RequestDispatcher; +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; + +@WebServlet(name = "dispatcher", urlPatterns = "/", loadOnStartup = 1) +public class DispatcherServlet extends HttpServlet { + private RequestMapping requestMapping; + private static final String REDIRECT_PREFIX = "redirect:"; + + @Override + public void init() throws ServletException { + requestMapping = new RequestMapping(); + } + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String url = req.getRequestURI(); + Controller controller = requestMapping.getController(url); + try { + String viewName = controller.execute(req, resp); + move(viewName, req, resp); + } catch (Exception e) { + System.out.println(e.getMessage()); + throw new ServletException(e.getMessage()); + } + } + + private void move(String viewName, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { + if (viewName.startsWith(REDIRECT_PREFIX)) { + resp.sendRedirect(viewName.substring(REDIRECT_PREFIX.length())); + return; + } + RequestDispatcher rd = req.getRequestDispatcher(viewName); + rd.forward(req, resp); + } +} diff --git a/src/main/java/core/mvc/ForwardController.java b/src/main/java/core/mvc/ForwardController.java new file mode 100644 index 00000000..0bca9f51 --- /dev/null +++ b/src/main/java/core/mvc/ForwardController.java @@ -0,0 +1,21 @@ +package core.mvc; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class ForwardController implements Controller { + + private final String forwardUrl; + + public ForwardController(String forwardUrl) { + this.forwardUrl = forwardUrl; + if (forwardUrl == null) { + throw new NullPointerException("forwardUrl is null"); + } + } + + @Override + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { + return forwardUrl; + } +} diff --git a/src/main/java/core/mvc/RequestMapping.java b/src/main/java/core/mvc/RequestMapping.java new file mode 100644 index 00000000..6059f905 --- /dev/null +++ b/src/main/java/core/mvc/RequestMapping.java @@ -0,0 +1,37 @@ +package core.mvc; + +import jwp.controller.*; + +import java.util.HashMap; +import java.util.Map; + +public class RequestMapping { + + private Map controllers = new HashMap<>(); + + public RequestMapping() { + initControllers(); + } + + private void initControllers() { + controllers.put("/", new HomeController()); + controllers.put("/user/signup", new CreateUserController()); + controllers.put("/user/list", new ListUserController()); + controllers.put("/user/login", new LoginController()); + controllers.put("/user/logout", new LogoutController()); + controllers.put("/user/update", new UpdateUserController()); + controllers.put("/user/updateForm", new UpdateUserFormController()); + + controllers.put("/user/form", new ForwardController("/user/form.jsp")); + controllers.put("/user/loginForm", new ForwardController("/user/login.jsp")); + controllers.put("/user/loginFailed", new ForwardController("/user/loginFailed.jsp")); + + controllers.put("/qna/form", new ForwardController("/qna/form.jsp")); + controllers.put("/qna/show", new ForwardController("/qna/show.jsp")); + + } + + public Controller getController(String url) { + return controllers.get(url); + } +} diff --git a/src/main/java/jwp/controller/CreateUserController.java b/src/main/java/jwp/controller/CreateUserController.java index 70f85f38..85995ea6 100644 --- a/src/main/java/jwp/controller/CreateUserController.java +++ b/src/main/java/jwp/controller/CreateUserController.java @@ -1,25 +1,22 @@ package jwp.controller; import core.db.MemoryUserRepository; +import core.mvc.Controller; import jwp.model.User; -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; -@WebServlet("/user/signup") -public class CreateUserController extends HttpServlet { +public class CreateUserController implements Controller { + @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { User user = new User(req.getParameter("userId"), req.getParameter("password"), req.getParameter("name"), req.getParameter("email")); MemoryUserRepository.getInstance().addUser(user); System.out.println("user 회원가입 완료"); - resp.sendRedirect("/user/list"); + return "redirect:/user/list"; } } \ No newline at end of file diff --git a/src/main/java/jwp/controller/HomeController.java b/src/main/java/jwp/controller/HomeController.java index 8c99a400..6ab2f4f6 100644 --- a/src/main/java/jwp/controller/HomeController.java +++ b/src/main/java/jwp/controller/HomeController.java @@ -1,18 +1,14 @@ package jwp.controller; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; +import core.mvc.Controller; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -@WebServlet("/") -public class HomeController extends HttpServlet { +public class HomeController implements Controller { + @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - RequestDispatcher rd = req.getRequestDispatcher("/home.jsp"); - rd.forward(req, resp); + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { + return "/home.jsp"; } } diff --git a/src/main/java/jwp/controller/ListUserController.java b/src/main/java/jwp/controller/ListUserController.java index c05d4198..934ebe24 100644 --- a/src/main/java/jwp/controller/ListUserController.java +++ b/src/main/java/jwp/controller/ListUserController.java @@ -1,28 +1,22 @@ package jwp.controller; import core.db.MemoryUserRepository; +import core.mvc.Controller; import jwp.util.UserSessionUtils; -import javax.servlet.RequestDispatcher; -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 javax.servlet.http.HttpSession; -import java.io.IOException; -@WebServlet("/user/list") -public class ListUserController extends HttpServlet { +public class ListUserController implements Controller { + @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { HttpSession session = req.getSession(); if (UserSessionUtils.isLogined(session)) { req.setAttribute("users", MemoryUserRepository.getInstance().findAll()); - RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); - rd.forward(req, resp); - return; + return "/user/list.jsp"; } - resp.sendRedirect("/user/login.jsp"); + return "redirect:/user/loginForm"; } } \ No newline at end of file diff --git a/src/main/java/jwp/controller/LoginController.java b/src/main/java/jwp/controller/LoginController.java index 8d7840db..0938a335 100644 --- a/src/main/java/jwp/controller/LoginController.java +++ b/src/main/java/jwp/controller/LoginController.java @@ -1,21 +1,17 @@ package jwp.controller; import core.db.MemoryUserRepository; +import core.mvc.Controller; import jwp.model.User; -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 javax.servlet.http.HttpSession; -import java.io.IOException; -@WebServlet("/user/login") -public class LoginController extends HttpServlet { +public class LoginController implements Controller { @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { HttpSession session = req.getSession(); String userId = req.getParameter("userId"); String password = req.getParameter("password"); @@ -23,11 +19,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S User loginUser = new User(userId, password); User user = MemoryUserRepository.getInstance().findUserById(userId); - if (user!= null && user.isSameUser(loginUser)) { + if (user != null && user.isSameUser(loginUser)) { session.setAttribute("user", user); - resp.sendRedirect("/"); - return; + return "redirect:/"; } - resp.sendRedirect("/user/loginFailed.jsp"); + return "redirect:/user/loginFailed"; } } diff --git a/src/main/java/jwp/controller/LogoutController.java b/src/main/java/jwp/controller/LogoutController.java index 82a0c152..57a2d1ff 100644 --- a/src/main/java/jwp/controller/LogoutController.java +++ b/src/main/java/jwp/controller/LogoutController.java @@ -1,20 +1,17 @@ package jwp.controller; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; +import core.mvc.Controller; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import java.io.IOException; -@WebServlet("/user/logout") -public class LogoutController extends HttpServlet { +public class LogoutController implements Controller { @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { HttpSession session = req.getSession(); session.removeAttribute("user"); - resp.sendRedirect("/"); + return "redirect:/"; } } diff --git a/src/main/java/jwp/controller/UpdateUserController.java b/src/main/java/jwp/controller/UpdateUserController.java index 7ea131ec..a94f022f 100644 --- a/src/main/java/jwp/controller/UpdateUserController.java +++ b/src/main/java/jwp/controller/UpdateUserController.java @@ -1,26 +1,22 @@ package jwp.controller; import core.db.MemoryUserRepository; +import core.mvc.Controller; import jwp.model.User; -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; -@WebServlet("/user/update") -public class UpdateUserController extends HttpServlet { +public class UpdateUserController implements Controller { @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { User modifiedUser = new User( req.getParameter("userId"), req.getParameter("password"), req.getParameter("name"), req.getParameter("email")); MemoryUserRepository.getInstance().update(modifiedUser); - resp.sendRedirect("/user/list"); + return "redirect:/user/list"; } } diff --git a/src/main/java/jwp/controller/UpdateUserFormController.java b/src/main/java/jwp/controller/UpdateUserFormController.java index 7fecdca2..dd08c270 100644 --- a/src/main/java/jwp/controller/UpdateUserFormController.java +++ b/src/main/java/jwp/controller/UpdateUserFormController.java @@ -1,22 +1,17 @@ package jwp.controller; import core.db.MemoryUserRepository; +import core.mvc.Controller; import jwp.model.User; -import javax.servlet.RequestDispatcher; -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 javax.servlet.http.HttpSession; -import java.io.IOException; -@WebServlet("/user/updateForm") -public class UpdateUserFormController extends HttpServlet { +public class UpdateUserFormController implements Controller { @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { String userId = req.getParameter("userId"); // 수정되는 user User user = MemoryUserRepository.getInstance().findUserById(userId); @@ -24,12 +19,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se Object value = session.getAttribute("user"); if (user != null && value != null) { - if (user.equals(value)){ // 수정되는 user와 수정하는 user가 동일한 경우 - RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); - rd.forward(req, resp); - return; + if (user.equals(value)) { // 수정되는 user와 수정하는 user가 동일한 경우 + return "/user/updateForm.jsp"; } } - resp.sendRedirect("/"); + return "redirect:/"; } } \ No newline at end of file diff --git a/webapp/home.jsp b/webapp/home.jsp index ac8aec83..5527b1dc 100644 --- a/webapp/home.jsp +++ b/webapp/home.jsp @@ -15,7 +15,7 @@
    - 객체지향을 가장 잘 다룬 책이 뭐가 있나요? + 객체지향을 가장 잘 다룬 책이 뭐가 있나요?
    @@ -34,7 +34,7 @@ diff --git a/webapp/include/navigation.jspf b/webapp/include/navigation.jspf index 90861e3d..78bcdc0e 100644 --- a/webapp/include/navigation.jspf +++ b/webapp/include/navigation.jspf @@ -28,7 +28,7 @@
    diff --git a/webapp/qna/show.jsp b/webapp/qna/show.jsp index 7e897691..aada9a33 100644 --- a/webapp/qna/show.jsp +++ b/webapp/qna/show.jsp @@ -43,7 +43,7 @@
  • - 목록 + 목록
  • From 6471c34003a7b24093786c036a1240a5a6ca6b35 Mon Sep 17 00:00:00 2001 From: lyouxsun Date: Sat, 12 Oct 2024 15:24:50 +0900 Subject: [PATCH 08/27] =?UTF-8?q?fix:=20=ED=95=9C=EA=B8=80=20=EC=9D=B8?= =?UTF-8?q?=EC=BD=94=EB=94=A9=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .run/WebServerLauncher.run.xml | 10 ++++++++++ webapp/include/navigation.jspf | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .run/WebServerLauncher.run.xml diff --git a/.run/WebServerLauncher.run.xml b/.run/WebServerLauncher.run.xml new file mode 100644 index 00000000..76dea6d6 --- /dev/null +++ b/.run/WebServerLauncher.run.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/webapp/include/navigation.jspf b/webapp/include/navigation.jspf index 78bcdc0e..e88381ad 100644 --- a/webapp/include/navigation.jspf +++ b/webapp/include/navigation.jspf @@ -1,3 +1,5 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +