본문 바로가기
Spring Boot

Spring boot 내장 Servlet Container

by 상국이 2022. 1. 29.
728x90

소스코드

public static void main(String[] args) throws LifecycleException {
    Tomcat tomcat = new Tomcat();		//톰캣 객체 생성
    tomcat.setPort(8080);				//포트 설정

    Context context= tomcat.addContext("/", "/");

    HttpServlet servlet = new HttpServlet() {		//서블릿 만들기
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter printWriter = resp.getWriter();
            printWriter.println("<html><head><title>");
            printWriter.println("hey tomcat");
            printWriter.println("</title></head>");
            printWriter.println("<body><h1>Hi tomcat</h1></body><html>");
        }
    };

    String servletName = "Hi servlet";

    tomcat.addServlet("/", servletName, servlet);					//톰캣에 서블릿 추가
    context.addServletMappingDecoded("/hello", servletName);		//컨텍스트에 서블릿 매핑

    tomcat.start();                 //tomcat server뜨는지 확인
    tomcat.getServer().await();     //요청을 기다림
}

* Spring-boot 에서는 위 소스코드가 하는 일을 자동설정을 통해 설정 및 실행함

> ServletWebServerFactoryAutoConfiguration ( 서블릿 웹서버 생성)

> DispatcherServletAutoConfiguration( 서블릿 만들고 등록)

728x90

'Spring Boot' 카테고리의 다른 글

Spring boot port  (0) 2022.01.31
spring boot 내장 웹 서버 변경  (0) 2022.01.31
Starter & AutoConfigure  (0) 2022.01.29
@SpringBootApplication  (0) 2022.01.29
Spring boot 원리  (0) 2022.01.01

댓글