Explain the concept of Spring Boot's embedded servlet containers.

Spring Boot's embedded servlet container feature allows a web application to run with an internal web server packaged directly inside the application. This eliminates the need to install, configure, and manage an external application server separately, making deployment and development significantly easier.

Key Points: • Spring Boot packages the application and web server together into a single executable JAR. • Supported embedded servlet containers include Tomcat, Jetty, and Undertow. • The application can be started using a simple java -jar command without external server setup.

Example: Traditional Spring Application:

Application Code ↓ WAR File ↓ External Tomcat Server ↓ Deploy and Configure

Spring Boot Application:

Application Code ↓ Embedded Tomcat ↓ Executable JAR ↓ java -jar app.jar

The application starts immediately with its own web server.

Default Embedded Server:

• Apache Tomcat (default) • Automatically configured by Spring Boot. • No additional setup required.

Other Supported Servers:

• Jetty • Undertow

These can replace Tomcat by changing project dependencies.

Code Example:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(
                Application.class, args);
    }
}

When the application starts:

• Spring Boot creates the ApplicationContext. • The embedded servlet container starts automatically. • The application begins listening for HTTP requests.

Typical Startup Process:

Application Start ↓ Create Spring Context ↓ Initialize Embedded Server ↓ Register Servlets and Filters ↓ Application Ready

Benefits:

• Simplified deployment. • Faster development setup. • Easier containerization using Docker. • Improved portability across environments. • Reduced infrastructure management.

Configuration Example:

server.port=9090

This changes the embedded server port from:

8080 → 9090

Additional Configurations:

server.servlet.context-path=/api
server.tomcat.max-threads=200

Replacing Embedded Tomcat:

Maven Example:

Exclude: spring-boot-starter-tomcat

Add: spring-boot-starter-jetty

Spring Boot automatically configures Jetty as the embedded server.

Real-World Example:

Microservices Architecture:

Each microservice contains:

• Business Logic • Embedded Web Server • Configuration

This allows every service to be deployed independently as a standalone executable JAR or Docker container.

Best Practices:

• Use executable JAR deployment for microservices. • Customize server properties based on workload. • Use Undertow or Jetty if specific performance characteristics are required. • Monitor server metrics using Spring Boot Actuator.

Interview Tip: A concise interview answer is: Spring Boot uses embedded servlet containers such as Tomcat, Jetty, or Undertow to package the web server directly inside the application. This allows applications to run as standalone executable JAR files without requiring an external application server, simplifying development, deployment, and scaling.