This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
A servlet should never have an explicit constructor. That guarantees that the JVM can instantiate it with the implicit no-arg constructor. The code that would normally go into the constructor in some other class should go in the init method of a servlet class.
Though Servlet is a class
It is run under an environment ie Web container
Whenever any component run under Environment it has a life cycle.
In case of servlet
1 .first container instantiates the servlet class
using no-arg constructor
(if it fails it gives InstantiationException...you can try this by making your constructor private)
2. Then container call init() method
(init() method can be use to create database connection etc)
3.then Container create HttpServletRequst and HttpServletResponse object and involk the Service() method using them
4.when web container is shut down (ie Tomcat etc) ,container calls destroy() method of servlet
Here step 1,2,4 are called only once in the lifecycle or servlet
where as service() method ie step 3 is called for every new request.
another example of class which runs under environment is
Applet where envioronment is Java enabled Browser