skip to content

What is the default embedded server in a Spring Boot servlet web application, and how does it get there?

level: juniorimportance: must knowfreq 70%

answer

  1. starter-web -> starter-tomcat transitive
  2. TomcatServletWebServerFactory auto-config
  3. embedded = inside JVM, fat JAR
  4. default port 8080
  5. servlet stack default only

basics

~10 s

Embedded Apache Tomcat. When you add spring-boot-starter-web, it transitively pulls in spring-boot-starter-tomcat, so Spring Boot auto-configures and starts an embedded Tomcat on port 8080 by default.

solid answer

~40 s

For a servlet-stack web app (spring-boot-starter-web), the default is embedded Apache Tomcat. The web starter depends on spring-boot-starter-tomcat, which puts Tomcat on the classpath; ServletWebServerFactoryAutoConfiguration then detects it and creates a TomcatServletWebServerFactory, so the app runs a self-contained Tomcat inside the JVM — no external server or WAR deployment needed. It listens on port 8080 unless server.port says otherwise. "Embedded" means the servlet container is a library bundled in your fat JAR and started programmatically in main(), rather than you deploying a WAR into a standalone Tomcat/JBoss. You can override the default by excluding spring-boot-starter-tomcat and adding the Jetty or Undertow starter instead.

code

xml · 8 lines
xml
<!-- Adding this one starter gives you embedded Tomcat by default -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-web transitively includes spring-boot-starter-tomcat.
     ServletWebServerFactoryAutoConfiguration then builds a
     TomcatServletWebServerFactory and starts Tomcat on port 8080. -->

go deeper

for a junior

Know it's embedded Tomcat on 8080, pulled in by spring-boot-starter-web.

for a middle

Explain the transitive dependency chain and that auto-config builds the factory.

for a senior

Name TomcatServletWebServerFactory / ServletWebServerFactoryAutoConfiguration and the classpath conditions.

for a principal

Frame it as a classpath-driven convention and contrast with reactive/Netty and WebApplicationType inference.

## What an embedded server is **Embedded server** means the HTTP/servlet container runs *inside* your application's JVM process, started from your `main()` method, rather than you packaging a WAR and deploying it into a separately installed application server. Spring Boot's signature move is the "fat JAR" (a.k.a. executable/über JAR): one runnable `.jar` that contains your code, all dependencies, *and* the web server. ## How Tomcat becomes the default 1. When you add the starter `org.springframework.boot:spring-boot-starter-web`, Maven/Gradle transitively pulls in `spring-boot-starter-tomcat`, which brings Tomcat's `tomcat-embed-core` jar onto the classpath. 2. At startup, Spring Boot auto-configuration — specifically `ServletWebServerFactoryAutoConfiguration` and its nested `EmbeddedTomcat` configuration — sees the Tomcat classes on the classpath (via `@ConditionalOnClass`) and no other `ServletWebServerFactory` bean defined, so it creates a `TomcatServletWebServerFactory` bean. 3. That factory builds and starts the embedded Tomcat when the `ServletWebServerApplicationContext` refreshes. Default port is **8080** (change with `server.port`). ## Why Tomcat specifically It is the most battle-tested, widely deployed servlet container; Spring Boot picks it as the safe default. It implements the Jakarta Servlet API (formerly javax.servlet), so it works with the blocking, thread-per-request servlet programming model — Spring MVC (`@Controller`, `DispatcherServlet`). ## Key gotcha — SERVLET vs REACTIVE - The Tomcat default applies to the *servlet* stack (Spring MVC). - If instead you use `spring-boot-starter-webflux` (reactive stack), the default is **Netty**, not Tomcat. The choice is driven by the `WebApplicationType` Spring Boot infers from the classpath. ## When to keep the default Almost always for MVC apps. Only swap it out for a specific reason (async performance, footprint, ops standardization). **Terms defined:** - *Servlet container* = software implementing the Servlet spec (Tomcat/Jetty/Undertow). - *Auto-configuration* = Boot's conditional beans that configure the app based on what's on the classpath. - *Starter* = a curated dependency bundle.

  • What class actually creates the embedded Tomcat, and what condition triggers it?
    ServletWebServerFactoryAutoConfiguration (nested EmbeddedTomcat config) creates a TomcatServletWebServerFactory bean, gated by @ConditionalOnClass on Tomcat classes and no user-defined ServletWebServerFactory bean present.
  • Is Tomcat still the default if you use spring-boot-starter-webflux?
    No. WebFlux is the reactive stack; its default embedded server is Netty (Reactor Netty), not Tomcat.

saying these in an interview costs you the question

  • Thinking you must install and deploy a WAR into an external Tomcat — Boot embeds it
  • Saying the default is Jetty or Netty for a plain MVC app
  • Believing the server is configured by XML rather than auto-configuration

context