What is a servlet and a servlet container (like Tomcat)?
Options
- A servlet handles HTTP requests; Tomcat runs servlets (Correct option)
- A servlet is a database server; Tomcat is a build tool
- A servlet is a JavaScript function run in the browser
- A servlet is a test file; Tomcat compiles Java code
Answer
A servlet is a small Java class that handles HTTP requests; a container such as Tomcat runs servlets, receives the HTTP traffic and calls the right servlet
Why
A servlet is a Java class that receives an HTTP request and writes a response. It is the base technology of Java web applications.
A servlet container (Tomcat, Jetty, Undertow) is the server that listens on a port, parses HTTP, manages threads and calls your servlets.
Spring MVC is built on one servlet, the DispatcherServlet. Classic apps were packaged as WAR files and deployed in a Tomcat; Spring Boot embeds Tomcat inside a runnable JAR.
Remember
Servlet = Java HTTP handler. Container = the server that runs it.