Mehran Junejo
Mehran Junejo

Reputation: 77

Why do we need Init() method in servlets when servlets has it Construtor?

in java Constructor is used for initialization why we need init() for initialization.... This question was asked in an Interview

Upvotes: 8

Views: 9476

Answers (7)

jumper rbk
jumper rbk

Reputation: 416

I used init method to prepare my PreparedStatement once, so that the next time my servlet is called, the prepared statement already created. Please note that when a servlet first loaded in the app server, the init method will be executed. The subsequent called to the servlet, the init method will be ignored. It's only called once for the lifecycle of the servlet

Upvotes: -1

Bhavya Tripathi
Bhavya Tripathi

Reputation: 21

  1. In JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface.

  2. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init().

  3. It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44854

The constructor is not part of the servlet lifecycle.

As per the javadocs

init and destroy, to manage resources that are held for the life of the servlet

and

The ServletConfig object can still be retrieved via getServletConfig().

Upvotes: 2

Aniket
Aniket

Reputation: 118

Init() method is called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests. This is the reason we need init() method.

Refer these links:

http://www.tutorialspoint.com/servlets/servlets-life-cycle.htm
http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html

Upvotes: 2

G.S.Tomar
G.S.Tomar

Reputation: 308

1) Constructors are used by "Web Container(e.g of Tomcat, WebSphere etc.) to instantiate GenericServlet/HttpServlet.

2) Role of "Servlet.init()" method is to inject defined in web.xml. Yes, you can define servlet level parameters in constructor as well, but then, as a developer you shall unnecessarily invest time in doing the same stuff which can be done implicitly for you by (Container+Servlet API).

3) Moreover, "Servlet.init()" is also used by web container to inject "ServletContext" object where you can't use constructor for that purpose.

Upvotes: 2

Farvardin
Farvardin

Reputation: 5424

the init() method is a part of Servlet and ServletConfig protocol. you can do what is related to the web-context in init() and what is private to Servlet class in constructor.

enter image description here

Upvotes: 5

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280175

The constructor is for normal Java initialization of an object (though typically a Servlet implementation is expected to have a no-arg constructor).

The init() method is a method provided by the Servlet interface which a Servlet container will run to configure the Servlet. The Servlet container will provide a ServletConfig object which gives the Servlet instance access to the ServletContext and other configuration elements from the deployment descriptor.

Upvotes: 5

Related Questions