
http://서버IP:포트/context/경로(식별값)getRequestURI() ➡ context/경로(식별값)getContextPath() ➡ contextgetRequestURI().substring(getContextPath().length) = 경로(식별값)private String getCommandName(HttpServletRequest request) {
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
return requestURI.substring(contextPath.length());
✅ Command 패턴 적용
Map<요청 식별값(String), Command 인터페이스 구현체>//FrontControllerServlet 틀
@WebServlet("/") // "/": 모든 요청을 다 받겠다
public class FrontControllerServlet extends HttpServlet {
//요청별로 Map을 가짐
Map<String, Command> getMap;
Map<String, Command> postMap;
public void init() {
getMap = new HashMap<>();
postMap = new HashMap<>();
}
//식별자 추출
private String getCommandName(HttpServletRequest request){
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
return requestURI.substring(contextPath.length());
}
private Command getCommand(HttpServletRequest request) {
String commandName = getCommandName(request);
Command command;
if(request.getMethod().equalsIgnoreCase("GET")) { //대소문자 구분없이 비교해라
command = getMap.get(commandName);
} else {
command = postMap.get(commandName);
} //Map에 없으면 404에러
return command;
}
public void execute(Command command, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String viewName = command.execute(request, response)
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Command command = getCommand(request);
if(command != null) {
execute(command, request, response);
} else { //404에러 처리
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
✅ 실제 요청을 처리하고 흐름을 제어
✅ 각 메서드는 Command 인터페이스와 일치하게 작성 (매개변수/리턴타입)
✅ 상속X, POJO
✅ 메서드 참조로 연결할 것
"/WEB-INF/views/" + 뷰 이름 + ".jsp""/WEB-INF/views/" & ".jsp" ➡ 공통 부분 - 뷰 이름만 알면 됨~!redirect:redirect url 형식/WEB-INF/views/404.jsp로 forward// localhost:8080/
@WebServlet("/") // "/": 모든 요청을 다 받겠다
public class FrontControllerServlet extends HttpServlet {
//요청별로 Map을 가짐
Map<String, Command> getMap;
Map<String, Command> postMap;
//접두어,접미어
String prefix = "/WEB-INF/views/";
String suffix = ".jsp";
//HomeController
HomeController homeController = new HomeController();
public void init() {
getMap = new HashMap<>();
postMap = new HashMap<>();
getMap.put("/", homeController::getIndex);
}
//식별자 추출
private String getCommandName(HttpServletRequest request){
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
return requestURI.substring(contextPath.length());
}
private Command getCommand(HttpServletRequest request) {
String commandName = getCommandName(request);
Command command;
if(request.getMethod().equalsIgnoreCase("GET")) { //대소문자 구분없이 비교해라
command = getMap.get(commandName);
} else {
command = postMap.get(commandName);
} //Map에 없으면 404에러
return command;
}
public void execute(Command command, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String viewName = command.execute(request, response);
if(viewName.startsWith("redirect:")) {
response.sendRedirect(viewName.substring("redirect:".length()));
} else {
String view = prefix + viewName + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Command command = getCommand(request);
if(command != null) {
execute(command, request, response);
} else { //404에러 처리
String view = prefix + "404" + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
}
public class TodoController {
public String getList(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<String> list = Arrays.asList("Todo 1", "Todo 2", "Todo 3");
request.setAttribute("todoList", list);
System.out.println("GET /todo/list");
return "todo/list";
}
public String getView(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("GET /todo/view");
return "todo/view";
}
public String getCreate(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("GET /todo/create");
return "todo/create";
}
public String postCreate(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("POST /todo/create");
return "redirect:/todo/list";
}
public String getUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("GET /todo/update");
return "todo/update";
}
public String postUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("POST /todo/update");
return "redirect:/todo/list";
}
public String postDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("POST /todo/delete");
return "redirect:/todo/list";
}
}
@WebServlet("/") // "/": 모든 요청을 다 받겠다
public class FrontControllerServlet extends HttpServlet {
//요청별로 Map을 가짐
Map<String, Command> getMap;
Map<String, Command> postMap;
//접두어,접미어
String prefix = "/WEB-INF/views/";
String suffix = ".jsp";
//HomeController
HomeController homeController = new HomeController();
//TodoController
TodoController todoController = new TodoController();
public void init() {
getMap = new HashMap<>();
postMap = new HashMap<>();
getMap.put("/", homeController::getIndex);
getMap.put("/todo/list", todoController::getList);
getMap.put("/todo/view", todoController::getView);
getMap.put("/todo/create", todoController::getCreate);
getMap.put("/todo/update", todoController::getUpdate);
postMap.put("/todo/create", todoController::postCreate);
postMap.put("/todo/update", todoController::postUpdate);
postMap.put("/todo/delete", todoController::postDelete);
}
//식별자 추출
private String getCommandName(HttpServletRequest request){
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
return requestURI.substring(contextPath.length());
}
private Command getCommand(HttpServletRequest request) {
String commandName = getCommandName(request);
Command command;
if(request.getMethod().equalsIgnoreCase("GET")) { //대소문자 구분없이 비교해라
command = getMap.get(commandName);
} else {
command = postMap.get(commandName);
} //Map에 없으면 404에러
return command;
}
public void execute(Command command, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String viewName = command.execute(request, response);
if(viewName.startsWith("redirect:")) {
response.sendRedirect(viewName.substring("redirect:".length()));
} else {
String view = prefix + viewName + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Command command = getCommand(request);
if(command != null) {
execute(command, request, response);
} else { //404에러 처리
String view = prefix + "404" + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@WebServlet 어노테이션 X//@WebServlet 어노테이션 X - 부모라서~!
//매번 이 클래스를 상속 받으면 됨니도~
public class DispatcherServlet extends HttpServlet {
//요청별로 Map을 가짐
Map<String, Command> getMap;
Map<String, Command> postMap;
//접두어,접미어
String prefix = "/WEB-INF/views/";
String suffix = ".jsp";
public void init() {
getMap = new HashMap<>();
postMap = new HashMap<>();
createMap(getMap, postMap);
}
protected void createMap(Map<String, Command> getMap, Map<String, Command> postMap) {
}
//식별자 추출
private String getCommandName(HttpServletRequest request){
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
return requestURI.substring(contextPath.length());
}
private Command getCommand(HttpServletRequest request) {
String commandName = getCommandName(request);
Command command;
if(request.getMethod().equalsIgnoreCase("GET")) { //대소문자 구분없이 비교해라
command = getMap.get(commandName);
} else {
command = postMap.get(commandName);
} //Map에 없으면 404에러
return command;
}
public void execute(Command command, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String viewName = command.execute(request, response);
if(viewName.startsWith("redirect:")) {
response.sendRedirect(viewName.substring("redirect:".length()));
} else {
String view = prefix + viewName + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Command command = getCommand(request);
if(command != null) {
execute(command, request, response);
} else { //404에러 처리
String view = prefix + "404" + suffix;
request.getRequestDispatcher(view).forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@WebServlet("/")
public class FrontControllerServlet extends DispatcherServlet {
//HomeController
HomeController homeController = new HomeController();
//TodoController
TodoController todoController = new TodoController();
@Override
protected void createMap(Map<String, Command> getMap, Map<String, Command> postMap) {
getMap.put("/", homeController::getIndex);
getMap.put("/todo/list", todoController::getList);
getMap.put("/todo/view", todoController::getView);
getMap.put("/todo/create", todoController::getCreate);
getMap.put("/todo/update", todoController::getUpdate);
postMap.put("/todo/create", todoController::postCreate);
postMap.put("/todo/update", todoController::postUpdate);
postMap.put("/todo/delete", todoController::postDelete);
}
}