Understanding Tomcat: The Bridge Between Your Java Application and the Web
When we start learning Spring Boot, we often hear words like Tomcat, Servlet Container, Embedded Server, and DispatcherServlet. These terms can be confusing at the beginning.
So, let’s understand:
- What is Tomcat?
- Why do we need Tomcat?
- How does Tomcat work with Spring Boot?
- What happens when we send an HTTP request?
What is Tomcat?
Apache Tomcat is a web server and servlet container that allows Java applications to handle HTTP requests and responses.
In simple words:
Tomcat is the program that helps your Java application communicate with the outside world.
A normal Java application can execute code, but it does not know how to receive requests from a browser or Postman.
For example:
public class PaymentService {
public void processPayment() {
System.out.println("Payment completed");
}
}This code works when called from another Java class, but how can a user call it using:
POST http://localhost:8080/paymentJava itself does not understand HTTP requests.
This is where Tomcat comes in.
Imagine you built a restaurant.
Your kitchen has chefs who prepare food. However, customers cannot directly enter the kitchen and communicate with chefs.
You need someone to:
- receive customer orders
- take those orders to the kitchen
- bring the prepared food back
Tomcat works like that waiter.
The mapping is:
The client communicates with Tomcat, and Tomcat communicates with your Java application.
How Tomcat Handles a Request
Suppose we have a Spring Boot controller:
@RestController
@RequestMapping("/payment")
public class PaymentController {
@PostMapping
public String pay() {
return "Payment successful";
}
}A user sends:
POST localhost:8080/paymentThe request flow is:
Client
|
| HTTP Request
v
Tomcat
|
v
DispatcherServlet
|
v
PaymentController
|
v
PaymentService
|
v
Response
|
v
Tomcat
|
v
ClientWhat Does Tomcat Actually Do?
1. Listens for HTTP Requests
Tomcat opens a port (usually 8080) and waits for incoming requests.
Example:
localhost:8080When a request arrives:
GET /usersTomcat receives it.
2. Converts HTTP Requests into Java Objects
HTTP is just text-based communication.
Example:
POST /payment{
"amount":100
}Tomcat converts this into Java objects like:
- HttpServletRequest
- HttpServletResponse
Then it passes them to Spring.
3. Manages Servlets
Tomcat is called a Servlet Container because it manages Java Servlets.
A Servlet is a Java component that handles HTTP requests.
In Spring MVC, the main servlet is:
DispatcherServletThe relationship is:
Tomcat
|
v
DispatcherServlet
|
v
ControllerTomcat manages the lifecycle of DispatcherServlet:
- Creating it
- Initializing it
- Handling requests
- Destroying it
4. Manages Threads
A web application can receive thousands of requests.
Get Sivasothy Tharsi’s stories in your inbox
Join Medium for free to get updates from this writer.
Tomcat manages threads to handle multiple requests.
Example:
Request 1 ---> Thread 1 ---> OrderController
Request 2 ---> Thread 2 ---> PaymentController
Request 3 ---> Thread 3 ---> InventoryControllerAfter completing a request, the thread returns to the thread pool and can be reused.
Tomcat in Traditional Java Applications
Before Spring Boot became popular, developers usually installed Tomcat separately.
The process was:
Tomcat Server
|
|
Deploy WAR File
|
|
Java Web ApplicationDevelopers had to:
- install Tomcat
- configure ports
- deploy applications manually
Tomcat in Spring Boot
Spring Boot changed this approach.
Spring Boot comes with an embedded Tomcat server by default.
When we add:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>we get:
- Spring MVC
- Jackson JSON support
- Embedded Tomcat
Now when we run:
SpringApplication.run()Spring Boot automatically starts Tomcat.
The flow becomes:
Spring Boot Application
|
v
Embedded Tomcat
|
v
Listening on port 8080We don’t need to install Tomcat separately.
Tomcat vs Spring Boot
A common beginner confusion is:
“Is Tomcat part of Spring?”
No.
They are different.
Tomcat
Responsible for:
- Receiving HTTP requests
- Managing connections
- Managing servlet lifecycle
- Sending responses
Spring Boot
Responsible for:
- Creating beans
- Dependency Injection
- Business logic
- Application structure
They work together:
Client
|
v
Tomcat
|
v
Spring Boot
|
v
Business LogicWhat Happens When You Start a Spring Boot Application?
When you execute:
java -jar application.jarSpring Boot does:
- Creates the Spring Application Context
- Scans and creates beans
- Starts embedded Tomcat
- Registers DispatcherServlet
- Opens port 8080
- Waits for requests
Now your application is ready to serve HTTP requests.
Conclusion
Tomcat is the bridge between the web and your Java application.
A Spring Boot application contains your business logic, but Tomcat provides the environment to receive and respond to HTTP requests.
In simple terms:
Spring Boot writes the application logic. Tomcat makes that application accessible through the web.
Understanding Tomcat helps you understand what happens behind the scenes every time you call a Spring Boot API.







