Constructors in a Servlet.


Web tier: servlets, JSP, Web frameworks: Constructors in a Servlet.

  1. Constructors in a Servlet. (4 messages)

    Why do we need a constructor for a Servlet class when we can use init method? I know constructors are called before the init method, but not able to figure out how initialising the variables in constructors help than doing the same in init method. Any info on this will be of great help.

    Thanks and Regards

    Threaded Messages (4)

  2. Constructors in a Servlet.[ Go to top ]

    Normally you would never write a constructor for a servlet.

    But as a servlet is a class like any other it will have one.
  3. Constructors in a Servlet.[ Go to top ]

    when u r in a servlet class constructor it is just like writing a constructor in a normal java .

    During the init mothod of the servlet you servlet object can use the services on the container
  4. Constructors in a Servlet.[ Go to top ]

    I was feeling unusually verbose. So I blogged on this...
  5. Constructors in a Servlet.[ Go to top ]

    First, it is not advisable to create a constructor in a Servlet other than the default.

    Second, the servlet container is managing the life-cycle of these servlet instances. Variables initialized inside the default constructor of a servlet will only be run once during servlet instantiation. Since servlet instance are actually reused, the constructor is not invoked anymore during reuse, but the init method is always called once the servlet is reused from the pool.