Picking Servlets over Common Gateway Interface

Software Engineer | Open Source Enthusiast | Mentor | Learner I love documenting stuff that I come across and find interesting. Hoping that you will love reading it and get to know something new :)
Search for a command to run...

Software Engineer | Open Source Enthusiast | Mentor | Learner I love documenting stuff that I come across and find interesting. Hoping that you will love reading it and get to know something new :)
No comments yet. Be the first to comment.
AI development often presents an illusion of standardization. When engineering structured workflows, JSON Schema is universally relied upon to force Large Language Models into returning predictable, t

Large language models (LLMs) like Llama, Qwen, and DeepSeek are transforming how software interacts with data. However, moving these models from a local experimental script to a highly available, mult

Artificial Intelligence (AI) models, especially large language models (LLMs) like GPT, Claude, or Gemini, have transformed how we interact with computers. Yet, terms like tokens, token limits, quotas, tokenizers, and parameters can seem confusing at ...

JavaScript development involves many concepts and tools that help developers organize, share, and reuse code effectively. For newcomers, terms such as library, framework, UMD, and CDN can seem technical, and understanding their roles is essential for...

Large language models (LLMs) such as GPT, LLaMA, and Falcon have billions of parameters. While these models are powerful, their size makes them challenging to run and adapt for specific use cases. Three key concepts help solve this problem: quantizat...

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.

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.
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.
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.
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.

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.

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 :)