File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why is there an init()method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why is there an init()method?" Watch "Why is there an init()method?" New topic

Why is there an init()method?

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
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).

SCJP 5.0(75%), SCWCD 5.0(88%)
Aditya Singh
Ranch Hand

Joined: Mar 06, 2008
Posts: 62
So that we can happly override it, without bothering to call super.init(ServletCOnfig)
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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.
I agree. Here's the link:
subject: Why is there an init()method?
It's not a secret anymore!