Servlet Unit-3 MID
Servlet Unit-3 MID
2
What is Servlet?
3
Servlet: Basic Terms
Before looking into Servlet, we will see some important keywords
about web application.
Web Client: We will call browsers (IE, Chrome, Mozilla
etc.) as a Client, which helps in communicating with the
server
Http Request
Http Response
4
Introduction
Servlet technology is used to create Dynamic web application
Servlet technology is robust and scalable .
Before Servlet, CGI (Common Gateway Interface) scripting
language was popular as a server-side programming language, but
there were many disadvantages of this technology.
5
How Servlet Works?
Why we need Servlet?
Nowadays everything is available on Internet.
Starting from e-banking, e-commerce everything is available
through Hey
internet. We call all these applications as Web
Server, I want to display given
applications.
name in my web page Sorry, I can’t do
that Dynamic
computation
Hi, I am Servlet.
But I have
Let me anyou
help
application
to display given
named
name inSERVLET,
your web
which
page. can process
your request
Client Server
Dynamic Response
7
Servlet Life Cycle
14
Servlet Life Cycle
init() destroy()
Servlet
In Service
Servlet Container
16
Servlet Life Cycle: init()
i. Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is
loaded when the first request for the servlet is received by the web container.
17
Servlet Life Cycle: init()
Syntax:
18
Servlet Packages
Package javax.servlet
Servlet interface needs to be
Interface
Implemented by
It provides implementation of methods
GenericServlet of Servlet interfaces.
extended by
Contains interface and abstract class for
Class
19
Servlet Life Cycle: Service()
The service() method is the main method to perform the actual
task.
The servlet container (i.e. web server) calls the service() method
to handle requests coming from the client( browsers) and to write
the response back to the client.
Each time the server receives a request for a servlet, the server
spawns a new thread and calls service.
20
Servlet Life Cycle: Service()
Syntax:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException
{
…
…
}
21
Servlet Life Cycle: Service()
The service() method checks the HTTP request type (GET, POST,
PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc.
methods as appropriate.
The doGet() and doPost() are most frequently used methods with
in each service request.
22
Servlet Life Cycle: Destroy()
The destroy() method is called only once at the end of the life
cycle of a servlet.
This method gives your servlet a chance to close
i. database connections,
ii. halt background threads,
iii. write cookie lists or hit counts to disk, and
iv. perform other such cleanup activities.
After the destroy() method is called, the servlet object is marked
for garbage collection.
23
Servlet Life Cycle: Destroy()
public void destroy()
{
// Finalization code...
}
24
Servlet Life Cycle
26