File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes init() and init(ServletConfig config) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() and init(ServletConfig config)" Watch "init() and init(ServletConfig config)" New topic

init() and init(ServletConfig config)

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a question about init() and init(ServletConfig config) both used. I am confused about which one is used in initializing servlerts?
Also, in servlet called InitDestroyCounter, there is a
FileReader reader = new FileReader("InitDestroyCounter.initial");
in the init(). I don't understand what is "InitDestroyCounter.initial"? There is no initParameter specified in the Web.xml for the servlet initDestroyCounter.
Thanks in advance.
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
The servlet API source code is publicly available. You can download the source from and find the answer to your question.
Here is javax.servlet.GenericServlet:

FileReader is a class in the package.
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
There are two versions of init: one that takes no arguments and one that takes a ServletConfig object as an argument. The first version is used when the servlet does not need to read any settings that vary from server to server.
The second version of init is used when the servlet needs to read server-specific settings before it can complete the initialization. For example the servlet need to know about database settings, password files, etc, so the init method takes a ServletConfig as an argument. ServletConfig has a getInitParameter method with which you can look up initialization parameters associated with the servlet, just as with the getParameter method used in the init method of applets.
This is from the Core Servlets and JavaServerPages from Marty Hall(a great book).
I hope this help.
Marcos Maia.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Cameron,
The overloaded no-argument init() method was added , so that if a developer carelessly overrides the method, he would still be able to use getServletConfig() top retreive the ServletConfig object.I think, this was added in JSDK Version 2.1
With the addition of this method, the GenericServlet class has init(ServletConfig) method calling init() at the end.The init() is empty in GenericServlet class.
So with this, you can safely include the initialization code in init() without having to worry about calling super.init(config)
Hope this helps.
Sandeep Desai

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 27, 2001).]
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thank you Phil, Marcos, Desai!
I agree. Here's the link:
subject: init() and init(ServletConfig config)
It's not a secret anymore!