WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Intermediate · question 40 of 100

What are the differences between Spring Boot’s embedded servlet containers (Tomcat, Jetty, and Undertow)?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

Spring Boot provides support for several embedded servlet containers including Tomcat, Jetty, and Undertow. These containers are built into the application, which means that you can run your application as a standalone executable without needing to install and configure a separate application server.

Here are some of the key differences between these containers:

1. Tomcat is the most widely used servlet container in the world. It is known for its stability and maturity. Tomcat provides support for the Servlet and JSP specifications, as well as support for WebSocket, JMX, and JNDI. If you have an existing Tomcat-based application, Spring Boot’s embedded Tomcat container is likely the best choice.

2. Jetty is another popular servlet container that is known for its speed and low memory usage. Jetty provides support for the latest Servlet and WebSocket specifications, as well as support for SPDY and HTTP/2. Jetty is particularly well-suited for high-throughput applications that require low latency.

3. Undertow is a relatively new servlet container that was developed by JBoss. It is designed for high-performance applications that require a low memory footprint. Undertow provides support for the Servlet and WebSocket specifications, as well as support for HTTP/2. Undertow is particularly well-suited for microservices-based architectures.

In terms of choosing which servlet container to use with Spring Boot, the decision ultimately depends on your specific use case. If you have an existing Tomcat-based application, Spring Boot’s embedded Tomcat container is likely the best choice. If you are building a new application that requires high throughput, Jetty may be the best choice. If you are building a microservices-based architecture and require a low memory footprint, Undertow may be the best choice.

Here’s an example of how to configure your Spring Boot application to use the embedded Tomcat container:

@SpringBootApplication
public class MyApplication {

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

   @Bean
   public TomcatServletWebServerFactory tomcatFactory() {
      return new TomcatServletWebServerFactory() {
         @Override
         protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
         }
      };
   }

   @Bean
   public ServletWebServerFactory servletContainer() {
      return new TomcatServletWebServerFactory();
   }

}

In this example, we are configuring a TomcatServletWebServerFactory bean that sets up a security constraint that requires all requests to be made over HTTPS. We are also configuring a default ServletWebServerFactory bean that uses the TomcatServletWebServerFactory by default.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics