package com.spring.controller.common;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
// FrontController
// @ ▶ .xml : 서블릿 컨테이너(톰캣, 웹 서버)
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private HandlerMapping handler;
private ViewResolver viewResolver;
// init-method = "init"
// 1. 멤버변수 초기화 역할
// 2. 생성자
// 3. setter
// 4. @Autowired
// 5. DI(의존주입)
// 6. init() 메서드
public void init() {
handler=new HandlerMapping();
viewResolver=new ViewResolver();
viewResolver.setPrefix("./");
viewResolver.setSuffix(".jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uri=request.getRequestURI();
String cp=request.getContextPath();
String commend=uri.substring(cp.length());
System.out.println("FC : "+commend);
Controller controller=handler.getController(commend);
String view = controller.execute(request, response);
if(view.contains(".do")) {
view=viewResolver.getView(view);
}
response.sendRedirect(view);
}
}
'개발일지 > 스프링' 카테고리의 다른 글
자바 스프링부트 웹 개발에서 중요한 디자인 패턴과 활용 (0) | 2024.10.29 |
---|---|
MyBatis 사용법 [gradle 사용] (0) | 2024.03.27 |
web.xml 서블릿 선언하기 (0) | 2024.03.06 |
스프링 컨테니어 (application.xml) 설정 (트랜잭션, 템플릿패턴) (1) | 2024.03.05 |
Spring application.xml 트랜잭션 적용 (0) | 2024.03.04 |
댓글