Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is there an init()method?

 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all
Please anybody tell me Why is there an init()method ? In other words ,why is not the constructor enough for initializing a servelet? Please explain in detail and with example if possible.

This is given in HFSJ at page no. 102 but i am unable to understand it(means not got it).
 
Aditya Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that we can happly override it, without bothering to call super.init(ServletCOnfig)
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Aditya's answer is correct, it's only half the story.

init() is not part of the Servlet interface; it merely makes it easier to work with GenericServlet and HttpServlet.

The method that is part of the Servlet interface is init(ServletConfig config). Its purpose it to pass a ServletConfig object to the servlet (which is needed to perform number of things in the servlet). Since an interface can't specify that there needs to be a constructor with this parameter, it was made a method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic