Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init(), init( ServletConfig config)

 
Durga Penmatsa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had written a HttpServlet, which has two init methods.
1.public void init() throws ServletException
2.public void init(ServletConfig config) throws ServletException.
I found that both the init methods got executed, why is that so?.
I thought only one of them will get executed.
Cheers
Durga
 
Tim Duncan
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bet that you have super.init(config) as the first line of your init(ServletConfig) method!! Try commenting that line out.
The reason we're advised to add super.init(config) is because GenericServlet.init(ServletConfig) stores a copy of the ServletConfig so that its getInitParameter method can forward the call to the ServletConfig object. If you override init(ServletConfig) in your own code you should add super.init(config) to preserve that functionality, as you have done. However ....
since people often forget to do this, another method init() is provided as a convenience. GenericServlet.init(ServletConfig) calls this. So, the upshot is ...
if you want to add functionality at init-time, you should do it by overriding init(). The default implementation of init(ServletConfig) will save the ServletConfig object and then call (your) init().

[This message has been edited by Tim Duncan (edited October 05, 2001).]
 
Durga Penmatsa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, got your point.
Cheers
Durga
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic