Servlet Architecture and Life-Cycle
Servlet Architecture and Life-Cycle
WHAT IS SERVLET
The servlet is a JAVA program that runs particularly inside JVM or {JAVA Virtual
Machine} on the Web server. It is generally used to make/create Dynamic web
applications and pages.
It is a technology that is used to develop Dynamic web applications. It uses the JAVA
language for its work of creating web pages. This technology is secure, scalable, and
robust because it uses JAVA technology which provides the same features.
ARCHITECTURE
Servlet is an API that provides many interfaces and classes including documentation.
Servlet is a class that extends the capabilities of the servers and responds to the incoming
Servlet is a web component that is deployed on the server to create a dynamic web page.
CGI (Common Gateway Interface)-
CGI technology enables the web server to call an
external program and pass HTTP request
information to the external program to process the
request. For each request, it starts a new process
• Before Servlets, CGI(Common Gateway Interface) programming was used to create web
applications. Here's how a CGI program works :
• User clicks a link that has a URL to a dynamic page instead of a static page.
• The URL decides which CGI program to execute.
• Web Servers run the CGI program in a separate OS shell. The shell includes the OS
environment and the process to execute code of the CGI program.
• The CGI response is sent back to the Web Server, which wraps the response in an HTTP
response and sends it back to the web browser.
DISADVANTAGES OF CGI
If the number of clients increases, it takes more time to send the response.
For each request, it starts a process, and the web server is limited to start
processes.
It uses platform dependent languages e.g. C, C++, perl.
ADVANTAGES OF SERVLET
There are many advantages of Servlet over CGI. The web container creates threads for handling the multiple
requests to the Servlet. Threads have many benefits over the Processes such as they share a common memory
area, are lightweight, cost of communication between the threads are low.
Better performance: because it creates a thread for each request, not process.
Portability: because it uses Java language.
Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc.
Secure: because it uses java language.
SERVLET LIFE CYCLE
Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes
through four stages,
•Loading a Servlet.
•Initializing the Servlet.
•Request handling.
•Destroying the Servlet.
•Loading a Servlet:
The first stage of the Servlet lifecycle
involves loading and initializing the
Servlet by the Servlet container.
init() method:
The service() method of the Servlet is invoked to inform the Servlet about the client
requests. This method uses ServletRequest object to collect the data requested by the client.
This method uses ServletResponse object to generate the output content.
destroy() method:
The destroy() method runs only once during the lifetime of a Servlet and signals the end of the
Servlet instance.
As soon as the destroy() method is activated, the Servlet container releases the Servlet
instance.