Tomcat is the web server that runs your Spring application and connects it to the browser.
What is a Servlet?
In Java web development, a Servlet is just a Java class that:
Receives an HTTP request and returns an HTTP response
Like this:
Browser → Servlet → Response (HTML / JSON)
Example:
GET /login
Servlet runs Java code and returns:
<h1>Welcome</h1>
But…
A servlet cannot run by itself
It needs a Servlet Container
What is a Servlet Container?
A Servlet Container is a software that:
✔ Starts your servlets
✔ Listens to HTTP requests
✔ Calls the right servlet
✔ Sends the response back
✔ Manages threads, memory, security
Servlet Container = Runtime environment for servlets
So we need something that:
- Opens port 8080
- Accepts browser requests
- Runs your servlet Java code
That “something” is called a Servlet Container
What is Tomcat?
Apache Tomcat is a Servlet Container
Tomcat’s job is:
Browser → Tomcat → Servlet → Response → Tomcat → Browser
Tomcat:
- Opens a port (like 8080)
- Understands HTTP
- Runs servlets
- Manages lifecycle of web apps
Without Tomcat, your Spring web app is just Java files — nobody can access it via browser.
Spring is a framework that helps you write Java code.
So Spring needs Tomcat to run on.
Think like this:
Spring Boot Application
↓
Runs inside Tomcat
↓
Tomcat runs inside JVM
What happens when you run a Spring Boot app?
When you click Run on:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Spring Boot:
- Starts Tomcat internally
- Registers Spring as a servlet
- Tomcat listens on port 8080
- Requests come in
- Tomcat forwards them to Spring
- Spring runs your controllers
When you create a Spring Boot Web project, this dependency is added automatically:
spring-boot-starter-web
Inside it is:
spring-boot-starter-tomcat
Spring Boot comes with Tomcat built-in by default
Spring Boot creates exactly ONE main servlet
It is called:
DispatcherServlet
This is Spring’s front controller.
What is DispatcherServlet?
It is a special servlet written by Spring.
Tomcat talks only to servlets.
So Spring creates one servlet and tells Tomcat:
“Send all requests to this servlet.”
So flow becomes:
Browser
↓
Tomcat
↓
DispatcherServlet (Spring)
↓
Your @Controller / @RestController
One DispatcherServlet
|
|-- "/login" → login()
|-- "/register" → register()
|-- "/products" → products()
So:
Spring has one servlet but thousands of routes
What happens internally when you hit a URL in Spring Boot?
You type this in browser
http://localhost:8080/hello
1️⃣ Browser sends HTTP request
Browser sends:
GET /hello
Host: localhost:8080
This goes to…
2️⃣ Tomcat (Web Server)
Tomcat is running because Spring Boot started it.
Tomcat:
- Is listening on port 8080
- Receives the HTTP request
- But Tomcat doesn’t know Spring controllers.
It only knows:
“I must send requests to a servlet”
Spring registered one servlet:
DispatcherServlet
So Tomcat forwards request to it.
3️⃣DispatcherServlet (Heart of Spring MVC)
DispatcherServlet receives:
GET /hello
4️⃣ HandlerMapping
Spring looks in HandlerMapping
This is just an internal routing table.
It finds:
/hello → HelloController.hello()
5️⃣ Spring calls your method
Your controller runs:
Now it asks:
“Which controller method should handle /hello?”
To answer that, Spring has built a map when app started:
/hello → hello()
/login → login()
/users → getUsers()
Annotation Returns
@Controller HTML page
@RestController JSON / Text / Data
@RestController = @Controller + @ResponseBody
Top comments (0)