*
The moose likes Servlets and the fly likes init() or init(ServletConfig sc)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() or init(ServletConfig sc)?" Watch "init() or init(ServletConfig sc)?" New topic
Author

init() or init(ServletConfig sc)?

Ravi Singh
Ranch Hand

Joined: Aug 02, 2004
Posts: 51
If you overide both the init() and init(ServletConfig sf) methods, which one is called when the serlvet is instantiated?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
first: init(ServletConfig sc)
then : init()

if init(ServletConfig sc) method fails then init() would not be executed.

As given in the docs:

init(ServletConfig sc) method is called by the servlet container to indicate to a servlet that the servlet is being placed into service
[ September 15, 2004: Message edited by: adeel ansari ]
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
The container will always call init(ServletConfig) before placing a servlet in service. init() is a convenience method provided in GenericServlet.

The init(ServletConfig) of GenericServlet stores the ServletConfig object it receives from the servlet container for later use. It then makes a call to init().

If you override both,
- container still calls the life cycle method init(ServletConfig)
- you are responsible for calling super.init(config)
- init() will not be called, unless your implementation of init(ServletConfig) calls it.

Sheldon
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
thanx for pointing out sheldon. i just put it in reverse order. and paste the statement in the doc below. i am editing it.

Moreover, documentation says:

init() is a convenience method which can be overridden so that there's no need to call super.init(config).

Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig()
[ September 15, 2004: Message edited by: adeel ansari ]
 
Don't get me started about those stupid light bulbs.
 
subject: init() or init(ServletConfig sc)?
 
Similar Threads
Final value in a Servlet
about GenericServlet
Init method with,without argument
Doubt in Servlets
passing ServletContext