Get all updates of Java4s
Follow Us: Email Newsletter

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

Servlets » On Jan 6, 2013 { 16 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


16 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:

    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..

  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 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 servlet and jsp


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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