Servlet Life Cycle Methods and Phases

Bharath Thippireddy
A free video tutorial from Bharath Thippireddy
IT Architect and Best Selling Instructor- 350000+ students
4.4 instructor rating • 30 courses • 404,566 students

Learn more from the full course

JDBC Servlets and JSP - Java Web Development Fundamentals

Create Java Web Applications from scratch in easy steps

08:45:32 of on-demand video • Updated February 2021

  • Develop Dynamic Web Applications
  • Learn the fundamentals of JDBC and using the different interfaces in the JDBC API
  • Learn , Create and Deploy Servlets
  • Initialize parameters dynamically
  • Learn how to manage sessions
  • Understand how JSPs can help to separate Web logic and functionality from page layout
  • Learn the meaning and importance of MVC
  • Explore how to make JSPs smaller and more powerful with JSTL, custom tags
  • Understand and Implement Connections Pooling
  • Secure you web application
  • Learn how to manage sessions
English if you have run a java standalone program then you know that the starting point for every java program is the main method which is the only method which in turn calls the other methods. We write whereas for a servlet there are three say lifecycle methods starting with the unit service and destroy and there are four life cycle phases associated with these three methods They are instantiation initialization servicing and destruction these lifecycle methods are called by the container so the web containers like Apache Tomcat will first go through the instantiation phase where they will create a instance of our servlet class by loading it into memory. So if we are working on an e-commerce application where we have a order servlet that takes the order from the web browser from the client and then it saves that order into the database the container will First load this order servlet class into memory. will create an instance of it. That is called the instantiation face. It will then call the unit method on servlet this is where we should write code which will be run only once. This unit method is called only once by the container. So if we want to create database connections this is a good place to put that code. Then it calls the service method this service method is all our business logic goes this is where we can write all the code that saves it out of the database or retrieve data from the database or any other processing logic. This Service method is called n times That is if there are 100 clients using hundred browsers they are sending hundred requests. This service method will be called a hundred times. So if there are thousand orders placed by different clients thousand clients then this method will be called thousand times finally the destroy as itself says it does the opposite of or we should put the code which does the opposite of what the unit method does. So putting the code into these methods is our responsibility but calling them is the containers responsibility. So this destroy is also called only once the servlet container calling the destroy method is nothing but destruction and the container cost the service method it's called Servicing So to summarize there are three life cycle methods for every servlet and for life cycle phases the servlet container is responsible for instantiating the servlet initializing it by calling the unit method and then servicing it. And finally destruction destroying the servlet as well.