0% found this document useful (0 votes)
33 views21 pages

Servlets

A servlet is a Java program that runs on a web server and responds to requests from clients. When a client sends an HTTP request, the web server starts the corresponding servlet which computes a response. Servlets have better performance than CGI scripts as they remain loaded in memory between requests. The lifecycle of a servlet involves initialization via init(), handling requests via service(), and destruction via destroy().

Uploaded by

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

Servlets

A servlet is a Java program that runs on a web server and responds to requests from clients. When a client sends an HTTP request, the web server starts the corresponding servlet which computes a response. Servlets have better performance than CGI scripts as they remain loaded in memory between requests. The lifecycle of a servlet involves initialization via init(), handling requests via service(), and destruction via destroy().

Uploaded by

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

UNIT-III

SERVLETS

9/15/20
Servers
 A server is a computer that responds to requests from a
client.
 Typical requests: provide a web page, upload or download a file,
send email
 Web server maps HTTP request to the corresponding servlet.

 Typically, your little computer is the client, and someone


else’s big computer is the server
 However, any computer can be a server
 It is not unusual to have server software and client software running
on the same computer

2
EX:
 Apache Tomcat web server
 Internet Information services
 Google web server
 PWS(personal web server)

3
Client & Server

4
HTTP
HTTP is a protocol that clients and servers use on the
web to communicate.
HTTP is a stateless protocol 
The client sends an HTTP request and the server
answers with an HTML page to the client, using
HTTP.

5
CGI Scripts
CGI stands for “Common Gateway Interface”

Client sends a request to server

Server starts a CGI script


client server
Script computes a result for server
and quits client
script
Server returns response to client
Another client sends a request
Server starts the CGI script again
Etc.

6
CGI

7
Servlets
A servlet is like an applet, but on the server side

Client sends a request to server

Server starts a servlet


client server
Servlet computes a result for
server and does not quit client
servlet
Server returns response to client
Another client sends a request
Server calls the servlet again
Etc.

8
servlet

9
Servlets vs. CGI scripts
Advantages:
◦ Running a servlet doesn’t require creating a separate
process each time
◦ A servlet stays in memory, so it doesn’t have to be
reloaded each time
◦ Performance is significantly better.
◦ Servlets are platform-independent because they are
written in Java.
Disadvantage:
◦ Less choice of languages (CGI scripts can be in any
language)

10
How a Servlet works
1.User sends request for a servlet by clicking a link that has URL to a servlet.

11
2. The container finds the servlet using deployment descriptor and
creates two objects :HttpServletRequest
HttpServletResponse

12
3.Then the container creates or allocates a thread for that request and
calls the Servlet's service()method and passes the request,
response objects as arguments.

13
4.The service() method, then decides which servlet
method, doGet() or doPost() to call, based on HTTP Request
Method(Get, Post etc) sent by the client. Suppose the client
sent an HTTP GET request, so the service() will call
Servlet's doGet() method.

14
5.Then the Servlet uses response object to write the
response back to the client.

15
6.After the service() method is completed the thread dies.
And the request and response objects are ready
for garbage collection.

16
How container handles the request
Life cycle of a servlet
The servlet loads & initializes it using the init( )
method.
The servlet handles one or more client requests using
service ( ) methods.
The servlet is deactivated when the destroy( )
method is called or when the server is terminated.

These three methods control the life cycle of a servlet.


They are invoked at a specific time by the server.
Loading Servlet Class : A Servlet class is loaded
when first request for the servlet is received by the
Web Container.
Servlet instance creation :After the Servlet class
is loaded, Web Container creates the instance of it.
Servlet instance is created only once in the life
cycle.
Call to the init() method : init() method is called
by the Web Container on servlet instance to
initialize the servlet.

20
Call to the service() method : The containers call
the service() method each time the request for
servlet is received. The service() method will then
call the doGet() or doPost() methods based on the
type of the HTTP request.
Call to destroy() method: The Web Container call
the destroy() method before removing servlet
instance, giving it a chance for cleanup activity.

21

You might also like