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

Introduction To Servlet Life Cycle-1

A servlet is a Java program that generates dynamic web content and is managed by a servlet container. The servlet container loads and instantiates servlets, initializes them by calling init(), services requests by calling service() which then calls doGet() or doPost(), and destroys servlets by calling destroy(). The servlet lifecycle is controlled by the container and includes loading, initialization, request servicing, and destruction.

Uploaded by

vivek_champion
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Introduction To Servlet Life Cycle-1

A servlet is a Java program that generates dynamic web content and is managed by a servlet container. The servlet container loads and instantiates servlets, initializes them by calling init(), services requests by calling service() which then calls doGet() or doPost(), and destroys servlets by calling destroy(). The servlet lifecycle is controlled by the container and includes loading, initialization, request servicing, and destruction.

Uploaded by

vivek_champion
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Servlet Life Cycle

What is a servlet?
A servlet is a Java technology based web component, managed by a container, that generates dynamic content. A servlet can be considered as a tiny Java program which processes user request and generates dynamic content. There are many other alternatives like php, asp .net or CGI, for developing dynamic web sites. Benefit of using servlets over other technologies is servlets are developed in Java, so it comes with all benefits of Java language and it is platform independent. Following are the some advantages of using servlets. (1) They are generally much faster than CGI scripts. (2) They use a standard API that is supported by many web servers. (3) They have all the advantages of the Java programming language, including ease of development and platform independence. (4) They can access the large set of APIs available for the Java platform.

What is a Servlet Container?


Servlet container (also known as servlet engine) is a runtime environment, which implements servlet API and manages life cycle of servlet components. Container is responsible for instantiating, invoking, and destroying servlet components. One example of container is Apache Tomcat which is an opensource container.

The Servlet API


The servlet API provides the interfaces and classes that support servlets. These interfaces and classes are grouped into two packages: javax.servlet, and javax.servlet.http.
Sr l t o fg e veCni

Sr l t e ve

Gn rc e ve e ei S r l t

Hp e ve t Sr l t t

Sr l t e us e veRq e t

Hp e veRq e t t Sr l t e us t

S r l t e p ne e veRs o s j v x e ve.* a a .s r l t
4

Hp e veRs o s t S r l t e p ne t j v x e ve.ht .* a a .s r l t t p

Servlet Life Cycle


The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps. If an instance of the servlet does not exist, the Web container
Loads the servlet class. Creates an instance of the servlet class. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.

Invokes the service method, passing a request and response object. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.

The life cycle of a servlet can be categorized into four parts:

Loading and Instantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet. Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist untill the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet. The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

Servicing the Request: After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.

Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like the init() method this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the servlet container not to sent the any request for service and the servlet releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection.

Things to remember?
init Executed once when the servlet is first loaded. Not called for each request. Service Called in a new thread by server for each request. Dispatches to doGet, doPost, etc. Do not override this method! doGet, doPost, doBlah Handles GET, POST, etc. requests. Override these to provide desired behavior. destroy Called when server deletes servlet instance. Not called after each request.

Why You Should Not Override service


The service method does other things besides just calling doGet You can add support for other services later by adding doPut, doTrace, etc. You can add support for modification dates by adding a getLastModified method The service method gives you automatic support for: HEAD requests OPTIONS requests TRACE requests Alternative: have doPost call doGet

You might also like