Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() or init(ServletConfig sc)?

 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you overide both the init() and init(ServletConfig sf) methods, which one is called when the serlvet is instantiated?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic