-
Notifications
You must be signed in to change notification settings - Fork 24
4주차 미션 / 서버 2조 박재하 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaepar
wants to merge
6
commits into
Konkuk-KUIT:main
Choose a base branch
from
jaepar:week4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import controller.Controller; | ||
| 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; | ||
|
|
||
| @WebServlet("/") | ||
| public class DispatcherServlet extends HttpServlet { | ||
| private RequestMapper requestMapper; | ||
| private static final String REDIRECT = "redirect:"; | ||
|
|
||
| @Override | ||
| public void init() throws ServletException { | ||
| requestMapper = new RequestMapper(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| String url = req.getRequestURI(); | ||
| Controller controller = requestMapper.getController(url); | ||
|
|
||
| String view = controller.execute(req, resp); | ||
| sendView(req, resp, view); | ||
| } | ||
|
|
||
| private static void sendView(HttpServletRequest req, HttpServletResponse resp, String view) throws IOException, ServletException { | ||
| if (view.startsWith(REDIRECT)) { | ||
| resp.sendRedirect(view.substring(REDIRECT.length())); | ||
| return; | ||
| } | ||
| req.getRequestDispatcher(view).forward(req, resp); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import controller.*; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class RequestMapper { | ||
| private static final Map<String, Controller> controllerMap = new HashMap<>(); | ||
|
|
||
| static { | ||
| controllerMap.put("/", new HomeController()); | ||
| controllerMap.put("/user/signup", new CreateUserController()); | ||
| controllerMap.put("/user/userList", new ListUserController()); | ||
| controllerMap.put("/user/login", new LoginController()); | ||
| controllerMap.put("/user/logout", new LogoutController()); | ||
| controllerMap.put("/user/update", new UpdateUserController()); | ||
| controllerMap.put("/user/updateForm", new UpdateUserFormController()); | ||
| } | ||
|
|
||
| public Controller getController(String url) { | ||
| return controllerMap.get(url); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package controller; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
|
|
||
| public interface Controller { | ||
| String execute(HttpServletRequest req, HttpServletResponse response); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package controller; | ||
|
|
||
| import core.db.MemoryUserRepository; | ||
| 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 jwp.model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class CreateUserController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| User user = new User(req.getParameter("userId"), | ||
| req.getParameter("password"), | ||
| req.getParameter("name"), | ||
| req.getParameter("email")); | ||
| MemoryUserRepository.getInstance().addUser(user); | ||
|
|
||
| return "redirect:/user/userList"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package controller; | ||
|
|
||
| import jakarta.servlet.RequestDispatcher; | ||
| 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; | ||
|
|
||
| public class HomeController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| return "/home.jsp"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package controller; | ||
|
|
||
| import core.db.MemoryUserRepository; | ||
| import jakarta.servlet.RequestDispatcher; | ||
| 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 jakarta.servlet.http.HttpSession; | ||
| import jwp.model.User; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
|
|
||
| public class ListUserController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| HttpSession session = req.getSession(); | ||
| Object value = session.getAttribute("user"); | ||
|
|
||
| if (value != null) { | ||
| Collection<User> users = MemoryUserRepository.getInstance().findAll(); | ||
| req.setAttribute("users", users); | ||
|
|
||
| return "/user/list.jsp"; | ||
| } | ||
|
|
||
| return "redirect:/user/login.jsp"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. navigation.jspf 파일의 comment에서 언급한 ForwardController 활용 시 리다이렉트 요청을 "redirect:/user/loginForm" 으로 보내어 파일의 위치가 노출되지 않을 것 같습니다! |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package controller; | ||
|
|
||
| import core.db.MemoryUserRepository; | ||
| 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 jakarta.servlet.http.HttpSession; | ||
| import jwp.model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class LoginController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| String userId = req.getParameter("userId"); | ||
| String password = req.getParameter("password"); | ||
|
|
||
| User findUser = MemoryUserRepository.getInstance().findUserById(userId); | ||
|
|
||
| if (findUser == null || !findUser.matchPassword(password)) { | ||
| return "redirect:/user/login_failed.jsp"; | ||
| } | ||
|
|
||
| HttpSession session = req.getSession(); | ||
| session.setAttribute("user", findUser); | ||
|
|
||
| return "redirect:/"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package controller; | ||
|
|
||
| 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 jakarta.servlet.http.HttpSession; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class LogoutController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| HttpSession session = req.getSession(); | ||
| session.removeAttribute("user"); | ||
|
|
||
| return "redirect:/"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package controller; | ||
|
|
||
| import core.db.MemoryUserRepository; | ||
| 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 jwp.model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class UpdateUserController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| User user = new User(req.getParameter("userId"), | ||
| req.getParameter("password"), | ||
| req.getParameter("name"), | ||
| req.getParameter("email")); | ||
|
|
||
| MemoryUserRepository.getInstance().update(user); | ||
|
|
||
| return "redirect:/user/userList"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package controller; | ||
|
|
||
| import core.db.MemoryUserRepository; | ||
| import jakarta.servlet.RequestDispatcher; | ||
| 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 jwp.model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class UpdateUserFormController implements Controller{ | ||
|
|
||
| @Override | ||
| public String execute(HttpServletRequest req, HttpServletResponse response) { | ||
| User user = MemoryUserRepository.getInstance().findUserById(req.getParameter("userId")); | ||
| req.setAttribute("user", user); | ||
|
|
||
| return "/user/updateForm.jsp"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
|
|
||
| <!doctype html> | ||
| <html lang="ko"> | ||
| <%@ include file="./include/header.jspf" %> | ||
| <body> | ||
| <%@ include file="./include/navigation.jspf" %> | ||
|
|
||
| <div class="container" id="main"> | ||
| <h2>Q&A</h2> | ||
| <div class="qna-list"> | ||
| <ul class="list"> | ||
| <li> | ||
| <div class="wrap"> | ||
| <div class="main"> | ||
| <strong class="subject"> | ||
| <a href="qna/show.jsp"> 객체지향을 가장 잘 다룬 책이 뭐가 있나요? </a> | ||
| </strong> | ||
| <div class="auth-info"> | ||
| <i class="icon-add-comment"></i> | ||
| <span class="time">2025-03-26 23:11</span> | ||
| <span clas="author">이영선</span> | ||
| <!-- <a href="./user/profile.html" class="author">이영선</a> --> | ||
| </div> | ||
| <div class="reply" title="댓글"> | ||
| <i class="icon-reply"></i> | ||
| <span class="point">12</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </li> | ||
| <li> | ||
| <div class="wrap"> | ||
| <div class="main"> | ||
| <strong class="subject"> | ||
| <a href="qna/show.jsp"> 객체지향에서 가장 중요하다고 생각하는 것이 무엇인가요? </a> | ||
| </strong> | ||
| <div class="auth-info"> | ||
| <i class="icon-add-comment"></i> | ||
| <span class="time">2025-03-27 23:55</span> | ||
| <span class="author">이윤정</span> | ||
| <!-- <a href="./user/profile.html" class="author">이윤정</a> --> | ||
| </div> | ||
| <div class="reply" title="댓글"> | ||
| <i class="icon-reply"></i> | ||
| <span class="point">8</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </li> | ||
| </ul> | ||
| <div class="row"> | ||
| <div class="col-md-5"></div> | ||
| <div class="col-md-5"> | ||
| <ul class="pagination" style="display:align-items-center;"> | ||
| <li class="page-item disabled"> | ||
| <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a> | ||
| </li> | ||
| <li class="page-item"><a class="page-link" href="#">1</a></li> | ||
| <li class="page-item active" aria-current="page"> | ||
| <a class="page-link" href="#">2</a> | ||
| </li> | ||
| <li class="page-item"><a class="page-link" href="#">3</a></li> | ||
| <li class="page-item"> | ||
| <a class="page-link" href="#">Next</a> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| <div class="col-md-2 qna-write"> | ||
| <a href="qna/form.jsp" class="btn btn-primary pull-right" role="button">질문하기</a> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script> | ||
| <script src="js/scripts.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <title>KUIT</title> | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"> | ||
| <link href="./css/styles.css" rel="stylesheet"> | ||
| </head> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redirect: 문자열에 대한 상수 선언 좋은 것 같습니다!