Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() methods in Generic Servlet

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx charles.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic