aspose file tools*
The moose likes Servlets and the fly likes init() methods in Generic Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() methods in Generic Servlet" Watch "init() methods in Generic Servlet" New topic
Author

init() methods in Generic Servlet

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
hi,

i am a little confused my book says class GenericServlet implements Servlet interface. it further says if i have to override the init() method
it is better to call super.init() while overidding the init() method.

I cannot understand which of the two init()'s method is refering to when it says overide. the overloaded init() of GenericServlet or init method of the ServletInterface.

Hope i am making sense.


regards
[ May 16, 2006: Message edited by: faisal usmani ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Always override the no-argument init() method... the reason is that the GenericServlet class implements the init(ServletConfig) to save the ServletConfig locally in an instance variable (it can later be obtained by invoking getServletConfig()). The init(ServletConfig) method in GenericServlet then delegates to the no-argument init() which is the one you should implement.

There is no need to invoke super.init() from your init() implementation; indeed, the purpose of this method is to prevent such necessities; from the API documentation:
public void init()
A convenience method which can be overridden so that there's no need to call super.init(config).


Hope that helps!


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
thanx charles.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: init() methods in Generic Servlet