0% found this document useful (0 votes)
46 views18 pages

Servlet Unit-3 MID

The document discusses the servlet API and provides an overview of servlets. It defines what a servlet is, the basic terms related to servlets, and describes the servlet lifecycle including the init, service, and destroy methods. It also discusses how servlets work and why they are needed for dynamic web applications.

Uploaded by

parthiv12052003
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)
46 views18 pages

Servlet Unit-3 MID

The document discusses the servlet API and provides an overview of servlets. It defines what a servlet is, the basic terms related to servlets, and describes the servlet lifecycle including the init, service, and destroy methods. It also discusses how servlets work and why they are needed for dynamic web applications.

Uploaded by

parthiv12052003
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/ 18

Unit-3

Servlet API and


Overview
1
Servlet
 It is used to create web application
 It is Developed by Sun Microsystem
 It is server side programming.

2
What is Servlet?

“ Servlet is java class


which
extends the functionality of web server
by
dynamically generating web pages.”

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

Server and Client (browser) will


communicate with each other with
the help of HTTP protocol.
Web Server is the one which takes the
Client client request, process the request and
Server
sends back the response.

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.

Changes with respect to time

1. To retrive server’s current DATE


and Time
2. News paper clippings
3. Online Shopping
e.g. Virtual Dressing Room
..
.

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

i. Servlet class is loaded.


ii. Servlet instance is
created.
iii. init() method is service()
invoked.

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.

A Web application runs within a


ii. Servlet instance is created
Web container of a Web server. Web
The web container creates the instance of acontainer
servlet afterprovides
loading theruntime
servlet
class. The servlet instance is created only onceenvironment.
in the servlet life cycle.

iii. Init() method is invoked


The web container calls the init method only once after creating the servlet
instance. The init method is used to initialize the servlet.

17
Servlet Life Cycle: init()
Syntax:

public void init(ServletConfig config)


throws ServletException
{
//initialization… A servlet configuration object used by a
} servlet container to pass information to a
servlet during initialization process.

18
Servlet Packages
Package javax.servlet
Servlet interface needs to be
Interface

implemented for creating any servlet. It


Servlet provides 3 life cycle methods.

Implemented by
It provides implementation of methods
GenericServlet of Servlet interfaces.
extended by
Contains interface and abstract class for
Class

HttpServlet servlet that understands HTTP protocol.


extended by Package: javax.servlet.http

MyServlet User defined Servlet 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

You might also like