Can we have Servlet Constructor?

Can we have Servlet Constructor?

Yes, you can have as per rule. It is executed even before init(). Then why generally we avoid constructor in Servlets. First let us write one simple example and see it is true or not; then we will go into details.

Example on Servlet Constructor


Servlet Constructor
Output screenshot on Servlet Constructor

By the above screen, you can conclude constructor is called.

Reasons for not preferring to write a Servlet Constructor

  1. init() is one of the life cycle methods called implicitly after container creates a Servlet object.
  2. The overloaded init() method takes an object of ServletConfig as parameter. This ServletConfig object represents the specific servlet in which init() it written. You can use super.init(config) to call super class init() method of HttpServlet. Creation of an object of ServletConfig, calling init() method, passing ServletConfig object are done automatically by the container.
    Constructor does not have this facility of accessing ServletConfig object. Or, constructor does not know what a ServletConfig is.
  3. You can throw ServletException with init() but not with a constructor.
  4. You cannot have parameterized constructor in objects created dynamically by some software as in Servlets.
  5. If you implement Servlet interface to write a Servlet (instead of extending HttpServlet), you cannot have constructor (in interface).
  6. If you write a constructor to open some database handles, you must have your own destructor also. Java does not support destructors. destroy() is one of the life cycle methods called automatically where you can close the handles. Moreover, API is consistent with init() and destroy() (like in Applets).
  7. To create a Servlet object, anyhow, Servlet requires a constructor. This you may create or container create.
  8. init() is equivalent to constructor. So better avoid constructor and use init() to get the advantage of life cycle managent.

Let us dig a little bit.

  1. Constructor is called before init() method. Servlet’s initialization data, <init-param>, is available with init() method by the support of ServletConfig passed as parameter. If the constructor requires any initialization information to do with the Servlet, it cannot have and may throw NullPointerException sometimes by the container.
  2. As long as you do not require any initialization that do not require ServletConfig or ServletContext, you can use as well constructor also.

2 thoughts on “Can we have Servlet Constructor?

  1. saurabh nainwal

    Please explain this.

    You cannot have parameterized constructor in objects created dynamically by some software as in Servlets.

    Why this is not possible

    1. S. Nageswara Rao, Corporate Trainer Post author

      Observe, when a servlet is loaded by the server software like Tomcat, it creates an object by calling default or no-parameterized constructor. If a parameterized constructor exists created by the programmer, how Tomcat knows what parameters are to be passed and the values of the parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *