0% found this document useful (0 votes)
56 views

Servlet Architecture and Life-Cycle

Uploaded by

Parth Vora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Servlet Architecture and Life-Cycle

Uploaded by

Parth Vora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Servlets

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

The user sends HTTP requests to the web server


The server has the web container containing servlet, which gathers data from the
database and creates a response.
The response created by servlet is sent through HTTP Response to the client browser.
ARCHITECTURE COMPONENT
Client
The client shown in the architecture above is primarily working as a medium who is sending out HTTP
requests over to the web server and again processing the response it gets back from the server. client here is
the web browser.
Web Server
Primary job of a web server is to process the requests and responses that a user sends over time and maintain
how a web user would be able to access the files that has been hosted over the server. The server we are
talking about here is a software which manages access to a centralized resource or service in a network..
There are precisely two types of webservers:
1.Static web server
2.Dynamic web server
Web Container
Web container is another typical component in servlet architecture which is responsible for communicating
with the servlets. Two prime tasks of a web container are:
• Managing the servlet lifecycle
• URL mapping
Servlet can be described in many ways, depending on the context.

Servlet is a technology which is used to create a web application.

Servlet is an API that provides many interfaces and classes including documentation.

Servlet is an interface that must be implemented for creating any Servlet.

Servlet is a class that extends the capabilities of the servers and responds to the incoming

requests. It can respond to any requests.

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.

Servlet class is loaded


The classloader is responsible for
loading the servlet class. The servlet
class is loaded when the first request
for the servlet is received by the web
container.
Initializing a Servlet:
After the Servlet is instantiated successfully, the Servlet container initializes the instantiated
Servlet object. The container initializes the Servlet object by invoking
the Servlet.init(ServletConfig) method which accepts ServletConfig object reference as
parameter.The Servlet container invokes the Servlet.init(ServletConfig) method only once,
immediately after the Servlet.init(ServletConfig) object is instantiated successfully. This method
is used to initialize the resources, such as JDBC datasource.
1.Now, if the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
Handling request:
After initialization, the Servlet instance is ready to serve the client requests. The Servlet container
performs the following operations when the Servlet instance is located to service a request :
1. It creates the ServletRequest and ServletResponse objects. In this case, if this is a HTTP
request, then the Web container
creates HttpServletRequest and HttpServletResponse objects which are subtypes of
the ServletRequest and ServletResponse objects respectively.
2. After creating the request and response objects it invokes the Servlet.service(ServletRequest,
ServletResponse) method by passing the request and response objects.
1.The service() method while processing the request may throw
the ServletException or UnavailableException or IOException.
Destroying a Servlet:
When a Servlet container decides to destroy the Servlet, it performs the following operations,
1. It allows all the threads currently running in the service method of the Servlet instance to
complete their jobs and get released.
2. After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance.
1.After the destroy() method is executed, the Servlet container releases all the references of this
Servlet instance so that it becomes eligible for garbage collection.
SERVLET LIFE-CYCLE METHODS
There are three life cycle methods of a Servlet :
init()
service()
destroy()

init() method:

The Servlet.init() method is called by the Servlet container


to indicate that this Servlet instance is instantiated
successfully and is about to put into service.
service() 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy