2. Servlet Life Cycle
• A servlet life cycle can be defined as the entire
process from its creation till the destruction.
• The servlet is initialized by calling the init()
method.
• The servlet calls service() method to process a
client's request.
• The servlet is terminated by calling the
destroy() method.
• Finally, servlet is garbage collected by the
garbage collector of the JVM.
3. Servlet life cycle contains five steps:
1. Loading of Servlet
2. Creating instance of Servlet
3. Invoke init() once
4. Invoke service() repeatedly for each client
request
5. Invoke destroy()
3
4. Step 1: Loading of Servlet
When the web server (e.g. Apache Tomcat) starts
up, the servlet container deploy and loads all the
servlets.
Step 2: Creating instance of Servlet
Once all the Servlet classes loaded, the servlet
container creates instances of each servlet class.
Servlet container creates only once instance per
servlet class and all the requests to the servlet are
executed on the same servlet instance.
Step 3: Invoke init() method
Once all the servlet classes are instantiated, the
init() method is invoked for each instantiated
servlet. 4
5. Step 4: Invoke service() method
Each time the web server receives a request for servlet,
it spawns a new thread that calls service() method.
If the servlet is GenericServlet then the request is
served by the service() method itself,
if the servlet is HttpServlet then service() method
receives the request and dispatches it to the correct
handler method based on the type of request.
Step 5: Invoke destroy() method
When servlet container shuts down(this usually
happens when we stop the web server), it unloads all
the servlets and calls destroy() method for each
initialized servlets. 5
6. Architecture Diagram:
The following figure depicts a typical servlet life-cycle
scenario.
First the HTTP requests coming to the server
are delegated to the servlet container.
The servlet container loads the servlet before
invoking the service() method.
Then the servlet container handles multiple
requests by spawning multiple threads, each
thread executing the service() method of a
single instance of the servlet.
6
9. How Servlet Works?
1) When the web server (e.g. Apache Tomcat) starts up,
the servlet container deploy and loads all the servlets.
2) Once the servlet is loaded, the servlet container
creates the instance of servlet class. For each
instantiated servlet, its init() method is invoked.
3) Client (user browser) sends an Http request to web
server on a certain port. Each time the web server
receives a request, the servlet container creates
HttpServletRequest and HttpServletResponse objects.
The HttpServletRequest object provides the access to
the request information and the HttpServletResponse
object allows us to format and change the http
response before sending it to the client. 9
10. The init() method :
• The init() method is designed to be called only
once.
• It is called when the servlet is first created,
and not called again for each user request.
• So, it is used for one-time initializations, just
as with the init() method of applets.
• The servlet is normally created when a user
first invokes a URL corresponding to the
servlet.
12. The service() method :
• 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 formatted
response back to the client.
13. • Each time if the server receives a request for a
servlet, the server issues a new thread and
calls service.
• The service() method checks the HTTP request
type (GET, POST, PUT, DELETE, etc.) and calls
doGet, doPost, doPut, doDelete, etc. methods
as appropriate.
14. signature of service()
public void service(ServletRequest request,
ServletResponse response) throws
ServletException, IOException
{
}
15. • The service() method is called by the
container and service method invokes doGet,
doPost, doPut, doDelete, etc. methods as
appropriate.
• So you have nothing to do with service()
method but you override either doGet() or
doPost() depending on what type of request
you receive from the client.
• The doGet() and doPost() are most frequently
used methods with in each service request.
16. signature of the doGet() method
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException
{
// Servlet code
}
17. signature of the doPost() method
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
{
// Servlet code
}
18. The destroy() method :
• 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
database connections, halt background threads,
write cookie lists or hit counts to disk, and
perform other such cleanup activities.
• After the destroy() method is called, the servlet
object is marked for garbage collection. The
destroy method definition looks like this:
• public void destroy() { // Finalization code... }
26. The basic terminology used in servlet
• HTTP
• HTTP Request Types
• Difference between Get and Post method
• Container
• Server and Difference between web server
and application server
• Content Type
• Introduction of XML
• Deployment
27. HTTP (Hyper Text Transfer Protocol)
• Http is the protocol that allows web servers
and browsers to exchange data over the web.
• It is a request response protocol.
• Http uses reliable TCP connections by default
on TCP port 80.
• It is stateless means each request is
considered as the new request. In other
words, server doesn't recognize the user by
default.
29. Http Request Methods
• Every request has a header that tells the status of the client.
• There are many request methods. Get and Post requests are
mostly used.
• The http request methods are:
• GET
• POST
• HEAD
• PUT
• DELETE
• OPTIONS
• TRACE
30. HTTP Request Description
GET Asks to get the resource at the requested URL.
POST
Asks the server to accept the body info attached. It is
like GET request with extra info sent with the request.
HEAD
Asks for only the header part of whatever a GET would
return. Just like GET but with no body.
TRACE
Asks for the loopback of the request message, for
testing or troubleshooting.
PUT
Says to put the enclosed info (the body) at the
requested URL.
DELETE Says to delete the resource at the requested URL.
OPTIONS
Asks for a list of the HTTP methods to which the thing
at the request URL can respond
31. What is the difference between Get and Post?
GET POST
1) In case of Get request, only limited
amount of data can be sent because
data is sent in header.
In case of post request, large amount
of data can be sent because data is
sent in body.
2) Get request is not secured because
data is exposed in URL bar.
Post request is secured because data is
not exposed in URL bar.
3) Get request can be bookmarked Post request cannot be bookmarked
4) Get request is idempotent. It means
second request will be ignored until
response of first request is delivered.
Post request is non-idempotent
5) Get request is more efficient and
used more than Post
Post request is less efficient and used
less than get.
32. Container
• It provides runtime environment for Java Web
or JavaEE (J2EE) applications.
• It performs many operations that are given
below:
• Life Cycle Management
• Multithreaded support
• Object Pooling
• Security etc.
33. Server
• It is a running program or software that
provides services.
• There are two types of servers:
• Web Server
• Application Server
34. • Web Server
• Web server contains only web or servlet container. It can be
used for servlet, jsp, struts, jsf etc. It can't be used for EJB.
• Example of Web Servers are: Apache Tomcat and Resin.
• Application Server
• Application server contains Web and EJB containers. It can be
used for servlet, jsp, struts, jsf, ejb etc.
• Example of Application Servers are:
• JBoss Open-source server from JBoss community.
• Glassfish provided by Sun Microsystem. Now acquired by
Oracle.
• Weblogic provided by Oracle. It more secured.
• Websphere provided by IBM.
35. Content Type
• Content Type is also known as MIME (Multipurpose internet Mail
Extension) Type. It is a HTTP header that provides the description about
what are you sending to the browser.
• There are many content types:
• text/html
• text/plain
• application/msword
• application/vnd.ms-excel
• application/jar
• application/pdf
• application/octet-stream
• application/x-zip
• images/jpeg
• video/quicktime etc.