You are on page 1of 9

 Java Servlet life cycle consists of a series of events that begins

when the Servlet container loads Servlet, and ends when the
container is closed down Servlet. A servlet container is the part of
a web server or an application server that controls a Servlet by
managing its life cycle.
 A servlet life cycle can be defined as the entire process from its
creation till the destruction. The following are the paths followed
by a servlet.
 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.
 Learn Professional Java Training in Chennai @ Greens
Technologys.
The init() Method
 The init technique is called just once. It is called just when the
servlet is made, and not required any client asks for thereafter. In
this way, it is utilized for one-time instatements, similarly as with
the init strategy for applets.

 The servlet is typically made when a client initially conjures a


URL comparing to the servlet, yet you can likewise indicate that
the servlet be stacked when the server is first begun.

 At the point when a user invokes a servlet, a solitary example of


each servlet gets made, with every client ask for bringing about
another string that is given off to doGet or doPost as proper. The
init() technique just makes or loads a few information that will be
utilized for the duration of the life of the servlet.

The init method definition looks like this:
public void init(ServletConfig config) throws ServletException {
// Initialization code...
}
The service() Method
 The administration() strategy is the principle technique to play out
the genuine errand. The servlet compartment (i.e. web server) calls
the administration() strategy to deal with demands originating
from the customer( programs) and to compose the arranged
reaction back to the customer.

 Each time the server gets a demand for a servlet, the server brings
forth another string and calls benefit. The administration()
technique checks the HTTP ask for type (GET, POST, PUT,
DELETE, and so forth.) and calls doGet, doPost, doPut, doDelete,
and so forth strategies as proper.
The service method definition looks like this:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}
The doGet() Method
 A GET request results from a normal request for a URL or from an
HTML form that has no METHOD specified and it should be
handled by doGet() method.
The doget method definition looks like this:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The doPost() Method
 A POST request results from an HTML form that specifically lists
POST as the METHOD and it should be handled by doPost()
method.
The dopost method definition looks like this:
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}

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.
The dostroy method definition looks like this:
public void destroy() {
// Finalization code...
}
The following figure depicts a typical servlet life-cycle scenario.
 Initially the HTTP asks for going to the server are designated to
the servlet compartment.
 The servlet holder stacks the servlet before summoning the
administration() strategy.
 At that point the servlet holder handles various demands by
producing different strings, each string executing the
administration() technique for a solitary occasion of the servlet.
 Servlets are Java classes which service HTTP requests and
implement the javax.servlet.Servlet interface.
 Web application developers typically write servlets that extend
javax.servlet.http.HttpServlet, an abstract class that implements
the Servlet interface and is specially designed to handle HTTP
requests.
 Among several Software Training Institute in Chennai. Greens
Technologys is the only software training institute that offers the
best Java training in Chennai with concrete examples.
 Our trainers work professionally from leading IT companies that
are able to provide hands-on training that will help you understand
the concepts in Java easily.
 Our training will help you get a great experience in technology
and turn your ideas into new applications.
 We limit the size of our lots, which will make the training sessions
more interactive and well structured.
 We've developed our course based on current software trends,
which will certainly help you clear certification and interviews
easily without any difficulty, so join us and be quickly placed.