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.
Joined: Oct 11, 2011
Yes Tim is right But you can create a default constructor ( with no parameter) if you need.
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