Just so you know: There is a separate forum for Servlet/JSP questions. To answer your question: No, you do not provide a constructor for a Servlet. If you have something that has to be done only once, override the init() method. Otherwise, you provide all the code necessary by overriding the doPost() or doGet() methods.
Yes. A servlet must have a no-argument constructor, either one you write yourself, or the one the compiler will insert automatically if there's no other constructor. If you supply one, it will be used.
But of course there's the "init(ServletConfig config)" method. Generally, it's better to do your initialization in your implementation of that.
Thanks Friends, But after reading this i'm a bit confused :yes or no
No, you do not provide a constructor for a Servlet
Yes. A servlet must have a no-argument constructor, either one you write yourself, or the one the compiler will insert automatically if there's no other constructor.
i guess,the default constructor is always there...rite [ July 15, 2005: Message edited by: jas oberai ]
SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
posted 13 years ago
When you start working with J2EE, you have a delicate balance between the specification (the API) and the actual implementation, which is provided by a specific application server. When you write a servlet, it is instantiated by the application server, not you. It is therefore not a good practice to override the default constructor, because you don't know what state other objects will be in when it is instantiated. The recommended practice is to place once-only code in the servlet's init() method and not override the constructor.
In terms of pure Java, though, Ernest is right: there is always a constructor, either provided by you or the compiler.