Newsletter |
Servlet Life Cycle in Java, Explanation of Servlet Life Cycle Methods
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 .:: | ||
Material is super pls send all java metiers in my mail
how to create file download application in servlet? pls reply me
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..
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…..
please send me the material of Servlet,Spring and Hibernate…
Thanking You ………….
The data u provided is excellent
HI SivaTeja,
Awesome man you 🙂
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?
I loved this tutorial…:)
You are just awesome. Really very good explaination. Thanks a lot.
please send me the material of Servlet,Spring and Hibernate…
Thanking You ………….
Nice Tutorials….
gud material.
please send me the material of Servlet,Spring and Hibernate…
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.
why won’t you prepare collections,abstract, and JSP concepts.
Thank you team your work helps me a lot.keep it up.
Nice Nice Tutorials please add big data Tutorials also
plz send material to abhishekmuthyam@gmail.com servlet and jsp
Can u explain the 3 ways of communicating servlet with database with example programs