The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Spring
Servlets
Which class is called a servlet?
A servlet is a Java class declared with class. It differs from an ordinary class in two places. extends HttpServlet is what qualifies the class to receive web requests, and @WebServlet("/hello") registers that a request the browser sends to the path /hello is handed to this class. You remove these two one at a time and send the same request, to see whether Tomcat calls the doGet of this class.
Checked against real execution · Deployed all 12 combinations on Tomcat 11.0.24, sent requests, and checked against the results
Concepts in this lab
- Servlet class
- A servlet class is a Java class that implements the Servlet interface. Instead of implementing the interface directly, you usually extend HttpServlet, which already implements it. The developer does not create an object of a servlet class with new. The servlet container creates it, and when a request arrives, the container calls a method of that object. The container creates only one object for each servlet declaration and keeps using it. Both the class and the object that the container created are called a servlet.
- HttpServlet
- HttpServlet is an abstract class in the servlet API that you extend to make a servlet that receives HTTP requests. It already implements the Servlet interface that every servlet must implement, so the developer only writes extends HttpServlet and overrides methods such as doGet or doPost. When a request arrives, the service method of HttpServlet looks at the HTTP method of the request and calls the matching doXxx method. If a request comes in for a doXxx method that was not overridden, an error response saying that the HTTP method is not supported is sent.
- @WebServlet
- @WebServlet is an annotation that declares a class as a servlet and writes the paths (URL patterns) that the servlet handles. When the servlet container deploys the application, it reads this annotation and pairs the paths with the servlet. At least one path must be written, and a class with this annotation must extend HttpServlet. Adding the annotation does not turn an ordinary class into a servlet. The same registration can also be written in a configuration file called web.xml.
- Servlet and servlet container
- A servlet is the Java standard interface that receives an HTTP request, processes it, and produces a response. A servlet container (such as Tomcat) accepts network connections, parses requests, calls the right servlet, and manages the servlet lifecycle and the request-handling threads. Spring MVC runs on top of a single servlet called DispatcherServlet.
Try these
- Default settings — A servlet that has both. When a request arrives, Tomcat calls doGet.
- Removing @WebServlet — It is a servlet class, but it has no path to handle, so it is never called.
- Removing extends HttpServlet — The path is registered, but the class is not a servlet, so it fails on the first request.
- Removing both — An ordinary class with the same method. Tomcat does not use this class.
- With 3 paths — You write one servlet class per path.
- Three paths on one class — There is only one doGet, so all three paths give the same response.
Running with the default settings
- The server starts. Tomcat scans the class files of the web application and looks for classes with @WebServlet. It finds it on the Hello class, so Tomcat writes the path and the class written there into the path table. No object is created yet.
- You type localhost:8080/hello in the address bar of the browser, and the browser sends a GET /hello request to the server.
- Tomcat looks for the path /hello in the path table. The Hello class is written there.
- Tomcat creates an object of the Hello class. The code we wrote has no line new Hello(). Tomcat creates the servlet object.
- Tomcat passes the request to this object. The code in HttpServlet sees that it is a GET request and calls the doGet that we wrote. We did not write the line that calls doGet either. The text that doGet writes to the response is "Hello".
- The browser receives a 200 response. The body is "Hello".
Result of this run
- A servlet is a Java class declared with class. It differs from an ordinary class in extends HttpServlet (the qualification) and @WebServlet (the registration of a path). With both, Tomcat creates the object and calls doGet when a request arrives.
- The code we wrote has no new and no line that calls doGet. A method of an ordinary class runs only when our code calls it, but a method of a servlet is called by Tomcat when a request arrives.
Code
// Registration: hand requests for this path to this class
@WebServlet("/hello")
// Qualification: a servlet class that can receive web requests
public class Hello extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain; charset=UTF-8");
resp.getWriter().write("Hello");
}
}
Assumptions of this model
The servlet container is Tomcat 11.0.24 (Jakarta Servlet 6.1), and the model assumes that the browser sends one GET request per path, one after another. The localhost:8080 in the paths is an example of running Tomcat on its default port on my own computer. All 12 combinations were checked against the result of compiling the same source as the code on the screen, putting it in the web application folder (WEB-INF/classes), starting a real Tomcat, and sending the requests. What was checked is whether the class is of the Servlet type, the path table of Tomcat, the status code and body of the response, the number of objects created, and the number of doGet runs. That every servlet implements the Servlet interface and that HttpServlet is an implementation of it follow chapter 2 of the Jakarta Servlet specification, and that a class annotated with @WebServlet must extend HttpServlet and must have at least one path written on it follows section 8.1.1 of the specification. The screen shows the qualification with extends HttpServlet alone, but the exact condition is to implement the Servlet interface, and extending HttpServlet is the most common way. A path can also be registered with the web.xml configuration file or in code instead of @WebServlet, but only @WebServlet is covered here. The code on the screen leaves out the import lines and the @Override that is commonly put on an overridden method. That a plain class with only @WebServlet gets a 500 for the first request and a 404 from the next request to the same registration on is not defined by the specification but is a behavior observed on this Tomcat version. The calls to init and service that Tomcat goes through from creating the object to reaching doGet are left out, and they are covered in the in-depth lab "Servlets".
Previous lab: AOP and proxies · Next lab: Controllers