Init method and Constructor in Servlet - Coddicted

Init method and Constructor in Servlet 3

Servlet is a Java class-a special java class and it does have a no-argument constructor.
  • When the container loads the servlet into the memory, the no-argument constructor is called.(Till now the servlet object is not a servlet but a normal POJO – plain old java object).
  • It becomes a servlet when it is given the reference to the servletContext object and servletConfig object.Hence for the no-argument constructor of the servlet it is too early in its life cycle to do initialization.Thus no-argument constructor is used for the instantiation and init() method is used for the initialization.
  • Also the constructor is only called once, where as the init and destroy methods are called each time the servlet is put into service or taken out of service, as the servlet container is free to put servlets into service and take them out of service whenever it chooses. So they can be called more than once. 
  • Also we need a no-argument constructor  since it is no use of calling the init method if you haven’t constructed an object to call it on yet.


Back To Top
Rate this post

3 thoughts on “Init method and Constructor in Servlet

  1. Reply redwing 財布 Oct 4,2013 10:12 am

    I and my guys happened to be going through the good tricks found on the blog while immediately got a terrible feeling I
    had not expressed respect to the blog owner for those strategies.
    The young men came certainly warmed to learn all of them and now have extremely been enjoying them.
    We appreciate you turning out to be really accommodating as well as for settling on such fabulous
    tips millions of individuals are really wanting to be informed on.
    My honest apologies for not expressing appreciation to sooner.

  2. Reply montblanc ボールペン Feb 16,2014 11:14 pm

    I抎 must verify with you here. Which is not something I normally do! I enjoy studying a post that may make people think. Additionally, thanks for permitting me to comment!

  3. Pingback: TrackBack

Leave a Reply