Servlet and ServletContainer
Servlet
Simply put, a Servlet is a class that handles requests, processes them and reply back with a response. They are basically Java programs that run inside the boundaries of a container. They are responsible for accepting a request, processing it, and sending a response back.
See HttpServletRequest, HttpServletResponse and HttpSession
For example, we can use a Servlet to collect input from a user through an HTML form, query records from a database, and create web pages dynamically.
Servlet technology is not limited to the HTTP protocol. In practice it almost always is, but Servlet is a generic interface and the HttpServlet is an extension of that interface – adding HTTP specific support – such as doGet and doPost, etc. Servlet technology is also the main driver a number of other web technologies such as JSP – JavaServer Pages, Spring MVC, etc.
ServletContainer
Servlets need to be registered with a container first, either JEE or Spring-based, so that the containers can pick them up at start-up.
Servlets are under the control of another Java application called a Servlet Container. When an application running in a web server receives a request, the Server hands the request to the Servlet Container – which in turn passes it to the target Servlet.
At start-up, ServletContainers create an object of ServletContext. The job of the ServletContext is to function as the server or container’s memory and remember all the servlets, filters, and listeners associated with the web application, as described in its web.xml or equivalent annotations. Until we stop or terminate the container, ServletContext stays with it.
Examples
- Tomcat server
- Jetty
Servlet Lifecycle - init service destroy
init()
In the beginning, the container instantiates a servlet by calling its init() method. Once its initialization is complete, the servlet is ready to accept incoming requests.
init method is designed to be called only once. If an instance of the servlet does not exist, the web container:
- Loads the servlet class
- Creates an instance of the servlet class
- Initializes it by calling the init method
The init method must complete successfully before the servlet can receive any requests. The servlet container cannot place the servlet into service if the init method either throws a ServletException or does not return within a time period defined by the Web server.
load-on-startup parameter
The servlet’s load-on-startup parameter plays an important role in determining when it starts. If this parameter has a value greater than zero, only then the server initializes it at start-up. If this parameter is not specified, then the servlet’s init() is called when a request hits it for the very first time.
service()
This method is only called after the servlet’s init() method has completed successfully.
The Container calls the service() method to handle requests coming from the client, interprets the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
The container directs incoming requests for processing in the servlet’s service() method. After that, it further delegates the request to the appropriate method such as doGet() or doPost() based on the HTTP request type.
destroy()
Called by the Servlet Container to take the Servlet out of service.
With destroy(), the container tears the servlet down, and it can no longer accept incoming requests.
This method is only called once all threads within the servlet’s service method have exited or after a timeout period has passed. After the container calls this method, it will not call the service method again on the Servlet.
Sample implementation
https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-http-demo