*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() and init(ServletConfig config)" Watch "init() and init(ServletConfig config)" New topic
Author

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 http://jakarta.apache.org/site/sourceindex.html and find the answer to your question.
Here is javax.servlet.GenericServlet:

FileReader is a class in the java.io package.
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" 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.
Regards,
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  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).]


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thank you Phil, Marcos, Desai!
 
jQuery in Action, 2nd edition
 
subject: init() and init(ServletConfig config)
 
Similar Threads
why init() method is not final?
Loading Servlet On Startup
init(servletconfig)
why super.init() is used??
init() and init(ServletConfig) doubt?