Reputation: 77
in java Constructor is used for initialization why we need init() for initialization.... This question was asked in an Interview
Upvotes: 8
Views: 9476
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
Reputation: 21
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.
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().
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
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
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
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
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.
Upvotes: 5
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