Servlet is a Java class that creates a dynamic content after processing the user request, which was
sent from the web browser. Hence, Java Servlet is a technology used in the creation of dynamic web applications.
Although Servlet is quite similar to other web server technology such as Common Gateway Interface(CGI) but Servlet has overcome the disadvantages of CGI.
For example - A Servlet may process one or even multiple concurrent user requests which can be HTTP based or non-http without overburdening or increasing the workload on the
server(which is a big issue with CGI based web applications).
How a Servlet processes a user request
Through a web browser, a user sends a requests to the Web Server. This request could be
asking for a resource from the web server or a request with the details of the form filled by the user and waiting
for an appropriate reply from the server etc.
On receiving the request, the Web Server also known as a Servlet container, finds the required Java Servlet
class in a pool of Servlets and creates an instance of this required Servlet to handle one or more user requests.
This Java Servlet class processes the user requests and send the appropriate response
back to the Web Server or Servlet Container, which ultimately send the response back to the client.
Features of Java Servlet Technology
Servlets are faster and efficient than Common Gateway Interface(CGI) a similar technology to build dynamic web applications.
Java has made sure that there is no illegal access to the memory locations without proper
authorization and no compromise with data integrity. As Servlets are written in Java,
hence, web applications written in Servlets are secure and Java Servlets cannot operate outside their domain.
Servlets are based in Java and Java programs are platform independent. Hence, Servlets
carrying the advantage of Java, can also be executed on any platform which makes then
platform independent. Java Servlet web applications can be executed on any Web Server.
With the CGI based web applications, on the arrival of a new user request, a new process
is created i.e. the more user requests, the more new processes are going to be created on the Server and this could be a very resource intensive operation and could put a lot of workload on the Server.
But a Servlet doesn't create a new process for each user request it receives. Instead, only an instance of a Servlet class
can be created which handles all user concurrently by multithreading and thus saving system's memory.
Comparing to the CGI based web applications, where multiple user requests leads to a heavy workload on the Server,
a Servlet based web application is fairly light as only one process on the Server is created which handles multiple user requests.