Have you ever wondered when a web server application can serve only static HTML pages, which component of it would be helping to serve dynamic requests? That component is a servlet. A servlet is part of a Java web application. A servlet container may run multiple web applications at the same time, each having multiple servlets running inside.
Servlets
A servlet is a Java class that extends the capabilities of servers that host applications accessed by means of a request-response programming model and are used to generate dynamic responses.
This technology is robust, efficient and scalable because of the java language being used to build it. Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side programming language.
Common Gateway Interface
CGI technology enables the web server to call an external program and pass HTTP requests to the external program to process it. For each request, it starts a new OS process which further leads to many issues.
Why Servlets?
There are many problems in CGI technology that are eradicated by the use of servlets:
If the number of clients increases, it takes more time for sending the response.
For each request, it starts a new OS process, and the web server is limited to start processes which also leads to poor memory management and less efficiency.
It uses platform-dependent languages like C, C++, etc., so it's not portable and can't handle session tracking and caching.
CGI is expensive when compared to servlets.
Container
Servlet doesn’t have the main method so they are controlled by another java application called container. Tomcat is an example of a container.
A servlet container is nothing but a compiled, executable program. It has the task of loading, initializing and executing servlets.
How a container serves requests?
In servlets, for each new request, a new thread is generated unlike new OS processes in CGI. It runs multiple threads to handle multiple requests to a single servlet.
When a user clicks requests some information. The container looks that request if the request is for the servlet. If so, the container creates two objects:
HttpServletResponse
HttpServletRequest
Container finds the correct servlet based on URL in the request, create or allocates a thread for that request and pass the request and response object to the servlet thread.
The container calls the servlet’s service() method, depending upon the type of request. The service() method calls either the doGet() or doPost() method.
doGet() or doPost() method generates the dynamic page and stuff the page into a response object.
When thread execution completes, the container converts the response object into an HTTP response and send it back to the client.
The thread either dies or returns to container-managed pool of threads. The response and request object references fall out of scope and are collected for garbage collection.
👋 Enjoyed this blog?
Reach out in the comments below or on LinkedIn to let me know what you think of it.
For more updates, do follow me here :)