• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

init methods of Servlets: need clarification

 
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a confusion about init() and init(ServletConfig conf) methods invocation.
My doubt is :-
which method is called in a Servlets life cycle? init() or init(ServletConfig conf) or both.
 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init()
 
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init(ServletConfig sc) is called which in turn calls init()....so if you override init() that's good enough....because it will always be called by init(ServletConfig sc)....However, if you override init(ServletConfig sc) you need to call super.init(sc) because the init(ServletConfig sc) method which you've overridden has to run(that's mandatory)
[ December 21, 2006: Message edited by: Sayak Banerjee ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sayak.
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if you override init(ServletConfig sc) you need to call super.init(ServletConfig sc) because the init(ServletConfig sc) method which you've overridden has to run(that's mandatory)



Are you saying that it is mandatory to call super.init(ServletConfig sc), if we override the init(ServletConfig sc) method?

If yes then i don't agree with you because i think init(ServletConfig sc) in super class only stores the ServletConfig object for you and if we override it with ours then ServletConfig will not be available to us and we shall have to store it ourself.

Please comment
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from servlet spec. 2.4 page 165

init()
public void init() throws ServletException
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() .
Throws: ServletException - if an exception occurs that interrupts the servlet�s normal operation

init(ServletConfig)
public void init(ServletConfig config) throws ServletException
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(ServletConfig) .
This implementation stores the ServletConfig object it receives from the
servlet container for later use. When overriding this form of the method, call super.init(config).
Specified By: Servlet.init(ServletConfig) in interface Servlet
Parameters: config - the ServletConfig object that contains configutation information for this servlet
Throws: ServletException - if an exception occurs that interrupts the servlet�s normal operation


[ December 21, 2006: Message edited by: Sayak Banerjee ]
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But these specs doesn't say that super.init(ServletConfig) call is mandatory while overriding init(ServletConfig).

Am I right?
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is mandatory....if you skip that, you won't get the ServletConfig or ServletContext objects in your servlet...you can try it out.

I hope you remember calling super.finalize() from your overridden finalizer block, don't you? You can think of it as something similar to that.
[ December 21, 2006: Message edited by: Sayak Banerjee ]
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sayak you are right that if we don't call super.init(ServletConfig) then we can't use getServletConfig() to get the ServletConfig object as i said in my previous post


if we override it with ours then ServletConfig will not be available to us and we shall have to store it ourself



but it doesn't mean that ServletConfig object will not be available to us. We can however store that in ServletConfig variable (Class level variable) and then can use it wherever we required. Here is the sample code



Hence it shows that super.init(ServletConfig) is not mandatory to be called. However if you also overwrite init() as well as init(ServletConfig) then you will have two options

1- Call super.init(ServletConfig)
Or
2 - Store ServletConfig Object and then call init() yourself
[ December 21, 2006: Message edited by: Ali Gohar ]
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah you can do it that way....but think....do you really want to do that? You'll be left with a servlet where some of the API methods won't work...you'll have to call all of these on the ServletConfig object.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You'll be left with a servlet where some of the API methods won't work...you'll have to call all of these on the ServletConfig object



Not understood it exactly. can you please explain, it will really help

Thanks
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean you won't be able to call these methods like getServletContext() or getInitParameter() directly....you'll have to say sc.getServletContext, sc.getInitParameter() etc.
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is all is a bit confusing..

hmmm...need to brush up a bit !
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sayak! It has been a wonderful learning here. But my piece of good practice is here ...

Always it is better to invoke the method with an object, instead of just a method call. I feel ...

getServletConfig().getInitPatameter(...) is legible than just getInitParameter(...).

After all, not all people who work on servlets are SCWCDs and not all people will know that GenericServlet implements Servlet and ServletConfig interfaces (though they have to know it )
[ December 26, 2006: Message edited by: Manikandan Jayaraman ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic