Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we use constructors instead of init() in servlets?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have one doubt abt servlet ..It is possible to use constuctors instead of init() in servelt .if we can use then how the web container will call that constuctor.

thanks
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor is called the first time a Servlet is used in a Web container. So you can do work in there. After the Servlet is created, the init method is called to do one time setup work. The purpose of the init method is to allow for setup work that needs to wait until the Servlet has been constructed.

Take a look at Servlet Life Cycle and Initializing a Servlet in the Java EE Tutorial for more information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic