
Apache Tomcat은 Java 기반의 오픈 소스 Web Application Server (WAS)로서, 다음과 같은 주요 특징을 갖는다.
Servlet 및 JSP 지원: Tomcat은 Jakarta Servlet과 Jakarta Server Pages 사양을 구현하여 Java 기반의 Web Application 개발과 실행을 지원한다.
❓ Java Servlet, Java Server Pages가 아니라 Jakarta? Java EE에서 Jakarta EE로 이름이 변경된 후의 명칭이다.
오픈 소스 SW: Apache Software Foundation에서 개발한 오픈 소스 프로젝트로, 누구나 무료로 사용하고 수정 및 배포할 수 있다. 또한 Java 기반으로 개발되어 있으며, Windows, Linux, macOS 등 다양한 운영체제에서 실행이 가능하다.
Spring Framework 프로젝트에서 Tomcat은 다음의 3가지 큰 역할을 갖는다.
DispatcherServlet을 실행하고 관리하는 역할을 한다. DispatcherServlet은 클라이언트의 요청을 받아서 처리하고, 적절한 Controller로 전달한 후, 응답을 생성하는 역할을 한다.
DispatcherServlet 동작 방식
HTTP 요청 및 응답 처리
애플리케이션 배포 및 실행
애플리케이션 생명주기 관리
<!-- Context Parameters -->
<!-- Application 어디서든 접근할 수 있는 설정값 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- ContextLoadListener -->
<listener>
<listener-class>mysite.web.ContextLoadListener</listener-class>
</listener> 이 경우 클래스(여기선 ContextLoadListener)를 따로 만들어서 세부 설정을 조정할 수 있다.import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
public class ContextLoadListener implements ServletContextListener {
public ContextLoadListener() {
}
public void contextInitialized(ServletContextEvent sce) {
// 컨텍스트에서 Listener를 가져온다.
ServletContext servletContext = sce.getServletContext();
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation"); // /WEB-INF/applicationContext.xml
System.out.println("Application[MySite02] starts... " + contextConfigLocation);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
하지만 최신 Spring 버전에서는 Java Config를 통해서 Listener를 등록한다. (@Configuration과 WebApplicationInitializer)public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
context.setServletContext(servletContext);
servletContext.addListener(new ContextLoaderListener(context));
}
}
Static Resource 처리
1. Spring Application 외에도 정적 자원(HTML, CSS, JS, img 등)을 클라이언트에 제공하는 역할도 수행한다.
1. WAS는 동적 + 정적 동작을 다 처리한다.
Spring 프로젝트에서는 우리는 어떻게 사용하고 있을까?
Tomcat은 우리 프로젝트에서 다음 단계로 수행이 된다.
ContextLoaderListener)와 서블릿(DispatcherServlet)이 초기화된다.하지만 이후에 발전된? Spring Boot에서는 이 톰켓을 내장한 채로 사용하게 된다.
그럼 Spring Boot에서는 톰켓을 내장해서 쓰는데 왜 그런가? 그 이유와 어떻게 내장해서 쓰는지 알아보자.
build.gradle 또는 maven xml 파일에 spring-boot-starter-web 의존성을 추가해준다. 이를 통해 톰켓을 내장하게 되며 설정을 초기화 한다.
8080을 기본값으로 설정하고 경로는 /로 설정하게 된다. <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
이후 Spring Boot에서는 바로 run() 메서드를 통해 애플리케이션을 실행만 해주면 된다.
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
추가 ) application.properties 또는 yml에서 커스머마이징이 가능하고 JAR 파일로 실행 가능하다.