From 98b9d702120f5cc96c25293246b316c2415ced61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A4=80=EC=9A=A9?= <1dlwnsdyd1@gmail.com> Date: Fri, 10 Oct 2025 18:20:22 +0900 Subject: [PATCH 01/16] =?UTF-8?q?4=EC=A3=BC=EC=B0=A8=20=EB=AF=B8=EC=85=98?= =?UTF-8?q?=20/=20=EC=84=9C=EB=B2=84=201=EC=A1=B0=20=EC=9D=B4=EC=A4=80?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java-webMVC.ipr | 402 +++++++++++++++-- java-webMVC.iws | 425 +++++++++++------- .../jwp/controller/CreateUserController.java | 59 +++ .../jwp/controller/ListUserController.java | 40 ++ .../java/jwp/controller/LoginController.java | 41 ++ .../java/jwp/controller/LogoutController.java | 24 + .../jwp/controller/UpdateUserController.java | 44 ++ .../controller/UpdateUserFormController.java | 46 ++ 8 files changed, 873 insertions(+), 208 deletions(-) create mode 100644 src/main/java/jwp/controller/CreateUserController.java create mode 100644 src/main/java/jwp/controller/ListUserController.java create mode 100644 src/main/java/jwp/controller/LoginController.java create mode 100644 src/main/java/jwp/controller/LogoutController.java create mode 100644 src/main/java/jwp/controller/UpdateUserController.java create mode 100644 src/main/java/jwp/controller/UpdateUserFormController.java diff --git a/java-webMVC.ipr b/java-webMVC.ipr index fddddb4..fca0a5c 100644 --- a/java-webMVC.ipr +++ b/java-webMVC.ipr @@ -1,79 +1,393 @@ - - - + + - + + + + + + - - + - + + + + + + + - - - + + + + - - + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + @@ -83,7 +397,7 @@ @@ -94,11 +408,11 @@ - + - + - + \ No newline at end of file diff --git a/java-webMVC.iws b/java-webMVC.iws index d5bc759..98fae4a 100644 --- a/java-webMVC.iws +++ b/java-webMVC.iws @@ -1,207 +1,304 @@ + + - - - - + - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/jwp/controller/CreateUserController.java b/src/main/java/jwp/controller/CreateUserController.java new file mode 100644 index 0000000..af669f2 --- /dev/null +++ b/src/main/java/jwp/controller/CreateUserController.java @@ -0,0 +1,59 @@ +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 { // 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); // repository에 저장 + System.out.println("User 회원가입 끝"); + resp.sendRedirect("/user/list"); + // /user/list를 받는 controller 생성 + + // super.doPost(req, resp); + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} diff --git a/src/main/java/jwp/controller/ListUserController.java b/src/main/java/jwp/controller/ListUserController.java new file mode 100644 index 0000000..fb6b4ec --- /dev/null +++ b/src/main/java/jwp/controller/ListUserController.java @@ -0,0 +1,40 @@ +package jwp.controller; + +import core.db.MemoryUserRepository; +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; +import java.util.Collection; + +@WebServlet("/user/list") +public class ListUserController extends HttpServlet { + private static final String USER_SESSION_KEY = "user"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + HttpSession session = req.getSession(false); + if (session == null) { + resp.sendRedirect(req.getContextPath() + "/user/login"); + return; + } + + User user = (User) session.getAttribute(USER_SESSION_KEY); + if (user == null) { + resp.sendRedirect(req.getContextPath() + "/user/login"); + return; + } + + MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); + Collection users = userRepository.findAll(); + req.setAttribute("users", users); + + RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); + rd.forward(req, resp); + } +} diff --git a/src/main/java/jwp/controller/LoginController.java b/src/main/java/jwp/controller/LoginController.java new file mode 100644 index 0000000..a65a52f --- /dev/null +++ b/src/main/java/jwp/controller/LoginController.java @@ -0,0 +1,41 @@ +package jwp.controller; + +import core.db.MemoryUserRepository; +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/login") +public class LoginController extends HttpServlet { + private static final String USER_SESSION_KEY = "user"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + RequestDispatcher rd = req.getRequestDispatcher("/user/login.jsp"); + rd.forward(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String userId = req.getParameter("userId"); + String password = req.getParameter("password"); + + MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); + User user = userRepository.findUserById(userId); + if (user != null && user.matchPassword(password)) { + HttpSession session = req.getSession(); + session.setAttribute(USER_SESSION_KEY, user); + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + resp.sendRedirect(req.getContextPath() + "/user/loginFailed.jsp"); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/controller/LogoutController.java b/src/main/java/jwp/controller/LogoutController.java new file mode 100644 index 0000000..f96b029 --- /dev/null +++ b/src/main/java/jwp/controller/LogoutController.java @@ -0,0 +1,24 @@ +package jwp.controller; + +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/logout") +public class LogoutController extends HttpServlet { + private static final String USER_SESSION_KEY = "user"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + HttpSession session = req.getSession(false); + if (session != null) { + session.removeAttribute(USER_SESSION_KEY); + } + + resp.sendRedirect(req.getContextPath() + "/"); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/controller/UpdateUserController.java b/src/main/java/jwp/controller/UpdateUserController.java new file mode 100644 index 0000000..8b97c96 --- /dev/null +++ b/src/main/java/jwp/controller/UpdateUserController.java @@ -0,0 +1,44 @@ +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 javax.servlet.http.HttpSession; +import java.io.IOException; + +@WebServlet("/user/update") +public class UpdateUserController extends HttpServlet { + private static final String USER_SESSION_KEY = "user"; + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + HttpSession session = req.getSession(false); + if (session == null) { + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + User sessionUser = (User) session.getAttribute(USER_SESSION_KEY); + String userId = req.getParameter("userId"); + if (sessionUser == null || !sessionUser.isSameUser(userId)) { + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + String password = req.getParameter("password"); + String name = req.getParameter("name"); + String email = req.getParameter("email"); + + User updateUser = new User(userId, password, name, email); + MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); + userRepository.changeUserInfo(updateUser); + + session.setAttribute(USER_SESSION_KEY, updateUser); + resp.sendRedirect(req.getContextPath() + "/user/list"); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/controller/UpdateUserFormController.java b/src/main/java/jwp/controller/UpdateUserFormController.java new file mode 100644 index 0000000..11db950 --- /dev/null +++ b/src/main/java/jwp/controller/UpdateUserFormController.java @@ -0,0 +1,46 @@ +package jwp.controller; + +import core.db.MemoryUserRepository; +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 { + private static final String USER_SESSION_KEY = "user"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String userId = req.getParameter("userId"); + + HttpSession session = req.getSession(false); + if (session == null) { + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + User sessionUser = (User) session.getAttribute(USER_SESSION_KEY); + if (sessionUser == null || !sessionUser.isSameUser(userId)) { + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); + User user = userRepository.findUserById(userId); + if (user == null) { + resp.sendRedirect(req.getContextPath() + "/"); + return; + } + + req.setAttribute("user", user); + RequestDispatcher rd = req.getRequestDispatcher("/user/updateForm.jsp"); + rd.forward(req, resp); + } +} \ No newline at end of file From 18c1f18c0bb01782b5fbde5dbf8cdbccfbac675d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A4=80=EC=9A=A9?= <1dlwnsdyd1@gmail.com> Date: Sat, 11 Oct 2025 16:16:36 +0900 Subject: [PATCH 02/16] =?UTF-8?q?2=EC=B0=A8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 ++ java-webMVC.ipr | 8 +- java-webMVC.iws | 76 ++++++++++--------- src/main/java/jwp/controller/Controller.java | 10 +++ .../jwp/controller/DispatcherServlet.java | 56 ++++++++++++++ .../jwp/controller/ForwardController.java | 19 +++++ .../java/jwp/controller/HomeController.java | 19 +++-- .../jwp/controller/ListUserController.java | 42 +++++++--- .../java/jwp/controller/LoginController.java | 51 +++++++++---- .../java/jwp/controller/LogoutController.java | 25 ++++-- .../java/jwp/controller/RequestMapping.java | 23 ++++++ .../jwp/controller/UpdateUserController.java | 53 ++++++++++--- .../controller/UpdateUserFormController.java | 54 +++++++++---- 13 files changed, 346 insertions(+), 95 deletions(-) create mode 100644 src/main/java/jwp/controller/Controller.java create mode 100644 src/main/java/jwp/controller/DispatcherServlet.java create mode 100644 src/main/java/jwp/controller/ForwardController.java create mode 100644 src/main/java/jwp/controller/RequestMapping.java diff --git a/build.gradle b/build.gradle index 8883efe..7133144 100644 --- a/build.gradle +++ b/build.gradle @@ -45,4 +45,9 @@ dependencies { test { useJUnitPlatform() +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } \ No newline at end of file diff --git a/java-webMVC.ipr b/java-webMVC.ipr index fca0a5c..669b23b 100644 --- a/java-webMVC.ipr +++ b/java-webMVC.ipr @@ -11,7 +11,11 @@ - + + + + + @@ -21,13 +25,13 @@ + - - - - - + + + + + + + + + + +