How does Spring Boot embed a web server and how does the embedded Tomcat work?
Understand how Spring Boot embeds a web server, how embedded Tomcat starts in-process, handles requests, and how to tune or swap it for Jetty or Undertow.
Expected Interview Answer
Spring Boot embeds a web server by packaging the servlet container as a library inside your application, so a plain java -jar starts Tomcat in-process instead of you deploying a WAR to an external server.
The spring-boot-starter-web dependency pulls in embedded Tomcat, and Boot's ServletWebServerApplicationContext detects a ServletWebServerFactory (TomcatServletWebServerFactory) during startup, uses it to create and start the container, and wires the DispatcherServlet into it. Tomcat runs on a thread pool of worker threads that accept sockets, parse HTTP, and dispatch each request to your controllers. You can swap Tomcat for Jetty or Undertow by excluding the Tomcat starter and adding another, and tune it through server.* properties like server.port and server.tomcat.threads.max.
- Single self-contained runnable jar
- No external server install or WAR deploy
- Consistent server version across environments
- Easy to containerize and scale
- Pluggable container: Tomcat, Jetty, or Undertow
- Server tuning via simple properties
AI Mentor Explanation
Embedded Tomcat is like a touring team that brings its own portable pitch and floodlights instead of relying on the host ground. Wherever they go, they set up and play immediately, just as a Spring Boot jar carries its own server and starts serving the moment you run it, needing no venue provided in advance.
Step-by-Step Explanation
Step 1
Add the web starter
spring-boot-starter-web brings in embedded Tomcat and Spring MVC as transitive dependencies.
Step 2
Boot detects a server factory
The ServletWebServerApplicationContext finds a TomcatServletWebServerFactory bean during refresh.
Step 3
Create and start the container
The factory builds a Tomcat instance, binds server.port, and starts its connector and worker thread pool.
Step 4
Register the DispatcherServlet
Boot wires the DispatcherServlet into the container so incoming requests route to your controllers.
Step 5
Serve requests
Worker threads accept sockets, parse HTTP, and dispatch each request to the matched handler method.
Step 6
Tune or swap
Adjust server.* properties, or exclude Tomcat and add Jetty/Undertow to change the container.
What Interviewer Expects
- Understanding that the server ships inside the jar
- Role of ServletWebServerFactory and the web application context
- How DispatcherServlet is wired into Tomcat
- Awareness of Tomcat's worker thread pool model
- Knowing Tomcat can be swapped for Jetty or Undertow
Common Mistakes
- Thinking you still deploy a WAR to an external Tomcat by default
- Believing the embedded server runs in a separate process
- Not knowing how to change the port with server.port
- Unaware that Jetty or Undertow are drop-in alternatives
- Confusing the thread-per-request model with a single-threaded loop
Best Answer (HR Friendly)
“Spring Boot packages a web server inside the application itself, so you can just run the program and it starts serving web pages, with no need to install or configure a separate server. By default that built-in server is Tomcat, and you can change it or tune it with a few settings.”
Code Example
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// Starts embedded Tomcat in-process
SpringApplication.run(DemoApplication.class, args);
}
}
// application.properties
// server.port=8081
// server.tomcat.threads.max=200
// server.tomcat.accept-count=100<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>Follow-up Questions
- How do you change the embedded server's port?
- How would you replace Tomcat with Jetty or Undertow?
- What is the DispatcherServlet's role in request handling?
- How does Tomcat's thread pool handle concurrent requests?
- When would you still deploy a traditional WAR instead?
MCQ Practice
1. Which starter provides embedded Tomcat by default?
spring-boot-starter-web transitively includes spring-boot-starter-tomcat, giving you embedded Tomcat.
2. Which property changes the embedded server's listening port?
server.port sets the port the embedded servlet container binds to at startup.
3. How do you switch from embedded Tomcat to Undertow?
You exclude spring-boot-starter-tomcat and add spring-boot-starter-undertow so Boot's factory uses Undertow.
Flash Cards
Where does the web server run in Spring Boot? — In-process, embedded inside the runnable jar via a servlet container library.
What builds the embedded container? — A ServletWebServerFactory such as TomcatServletWebServerFactory, used by the web application context.
How does Tomcat handle requests? — A pool of worker threads accepts sockets, parses HTTP, and dispatches to the DispatcherServlet.
How do you change the port? — Set server.port in application.properties or as an environment variable.
Alternatives to embedded Tomcat? — Jetty and Undertow, added by excluding the Tomcat starter and including theirs.
Continue Learning
Related Interview Questions
What is the DispatcherServlet and how does the Spring MVC request flow work?
medium
What is the difference between @Controller and @RestController in Spring?
easy
How does exception handling work in Spring MVC with @ControllerAdvice?
medium
What is dependency injection in Spring and how does the IoC container work?
medium