I think default constructor is provided by the container which will be used for the creation of single instance of the servlet and if you want to explicit write your own constructor then it should be public and no argument . we can't write constructor with argument.
init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests
The constructor is only called once, where as the init and destory methods are called each time the servlet is put into service or taken out of service. If the servlet is programmed correctly, then the destroy method should undo (release) everything the init method did (allocated).
Well, obviously because the container will never use a constructor with arguments. It's your turn to say something sensible now: so explain why you think it is possible the container could use such a constructor.
The "more detailed format" you're asking about is the Servlet Specification. I suggest you read through that document -- since you're preparing for this certification exam you should be in the habit of studying the specifications, right? -- and see what it says about construction of servlet instances.
I am not asking about specification. I have gone through it but what exact error or exception(anything else to that I might be unaware of) that it gives when we create constructor with arguments that I am not getting.
posted 6 years ago
It's easy enough to test what happens if your servlet only has a constructor that takes arguments. I think it's safe to say that if you don't know why a servlet container can't use such a constructor, then you have indeed not spent enough time reading the servlet spec, and, just as importantly, not spent enough time developing servlets.
Servlets are java classes like a normal classes.
So You have a constructor to make a new Instance of the new Servlet class.
To become a Servlet object You need the Init method.
The container call the init method and make other think like Create a SerletConfig object, used for example to access the init parameter in the web.xml, and so on.
Actually it's not you, but servlet container who creates a servlet and calls init(). Servlet container is created in such way, that it calls init() (not constructor) for initialization. Servlet container calls constructor, but not for initialization purpose. Your question is for servlet container developer.