Newsletter

Servlet Life Cycle in Java, Explanation of Servlet Life Cycle Methods

Servlets » on Jan 6, 2013 { 18 Comments } By Sivateja

Aware of servlet life cycle is very important, before you going to execute first application. So let us see flow of life cycle methods…

Java servlet will be executed in different stages, of which

  • Loading the application and its Instantiation, Initialization
  • Executing the business logic
  • Finally destroying

Let us see whats going in these stages 🙂

We know that, javax.servlet.Servlet is the super interface, having the methods…

  • init(ServletConfig config)
  • service(ServletRequest req,ServletResponse res)throws ServletException,IOException
  • destroy()

Loading the application and its Instantiation,Initialization

In the first request, the container will creates the object of the servelt and executes the init() method first, remember this init() method will be executed only once…

  • At the time of giving the first request
  • If we use ‘load-on-startup‘ tag in the web.xml then, our servelt will be loaded and init() method will be executed at the time of server starts, will see about this ‘load-on-startup‘ in the upcoming articles
  • In general, database connection opening or any one time object creation tasks will be written in this method, as init() will be executed only once, hope you got one idea 😉

Executing the business logic

So business logic, which is in service(-,-) method, when ever we send the request to the servlet, every time this service() method will be executed.  If you send 100 requests to the servlet, 100 times service(-,-) method will be executed but init() method ? only one time :-).  This service() method internally calls doGet(-,-) or doPost(-,-) methods in HttpServlet, i will explain about this in the examples later, just remember as of now.

Finally destroying

This destroy() method will be called before servlet is removed from the container, and finally it will be garbage collected as usual.

Friends, just remember all this concept, i will explain line by line execution flow in the first servlet program, there i will touch every thing for you again 🙂

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

18 Responses to “Servlet Life Cycle in Java, Explanation of Servlet Life Cycle Methods”
  1. seenu.k says:

    Material is super pls send all java metiers in my mail

  2. nikhilesh says:

    how to create file download application in servlet? pls reply me

  3. Supriyo says:

    Hello,
    I have few confusions..
    1. Where is constructor. I must be declared before Init().???
    2. Does container really created an object of Servlet??
    3. Why shud we define init() to initialize the instance of servlet when we are having constructor??

    Thanks in advance..

    • Ahmad says:

      yes of course servlet container itself creates the object for servlet, servlet knows when the object has been created and if we want to perform some post construsted operation , but if we want to create the object with our user defined value, that's not possible in contruster so init() method comes into picutre, in this scenario we can can pass our own value by using init-param in web.xml……
      i think it will help u…..

  4. Naresh says:

    please send me the material of Servlet,Spring and Hibernate…

    Thanking You ………….

  5. gowda says:

    The data u provided is excellent

  6. Krupa says:

    HI SivaTeja,

    Awesome man you 🙂

  7. hii sir ,i have one small doubt.i.e,init(-) method is not factory method.but it’s creating only one object at time client request.but service(-,-) method call’s multiple times.but both are servlet life cycle methods.how it is?

  8. pradeep says:

    I loved this tutorial…:)

  9. Abhi says:

    You are just awesome. Really very good explaination. Thanks a lot.

  10. chandana says:

    please send me the material of Servlet,Spring and Hibernate…

    Thanking You ………….

  11. chandana says:

    Nice Tutorials….

  12. Ashok Kumar Penamalli says:

    gud material.
    please send me the material of Servlet,Spring and Hibernate…

  13. sasha says:

    kudos to your Good work. Could you please reduce number of redirection advertisement links. Lots of time it is geting redirected to ad page when trying to click on next.

  14. Narasimha says:

    why won’t you prepare collections,abstract, and JSP concepts.

    Thank you team your work helps me a lot.keep it up.

  15. Ajit kumar singh says:

    Nice Nice Tutorials please add big data Tutorials also

  16. abhishek says:

    plz send material to abhishekmuthyam@gmail.com servlet and jsp

  17. pushpa says:

    Can u explain the 3 ways of communicating servlet with database with example programs

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.