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
Thanks. But again we can use the constructor instead of init, for what we use init. Will it make any difference? Again why we can't pass any argument to constructor?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
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).
Thanks for reply. But upto my knowledge the init and destroy are also called once then.......and what about the constructor with argument?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
1
But upto my knowledge the init and destroy are also called once
The servlet container is free to put servlets into service and take them out of service whenever it chooses; that's what servlet spec says. So they can be called more than once.
and what about the constructor with argument?
It can't ever be used, so there's no point in having one.
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.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
1
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.
Hello,
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.
In order to intialise ServletConfig and ServletContext, the constructor is too early in the life cycle of Servlet. Init method is called only once during the life cycle of servlet.
Ashwin Sridhar
SCJP | SCWCD | OCA
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
massimiliano cattaneo wrote:So You have a constructor to make a new Instance of the new Servlet class.
Yes, but no initialization should take place in the constructor. So it's better not to have a constructor at all, and thus have the compiler insert the default no-arg constructor.
Ekaterina Galkina
Greenhorn
Joined: Sep 14, 2010
Posts: 15
posted
0
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.