aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init methods of Servlets: need clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init methods of Servlets: need clarification" Watch "init methods of Servlets: need clarification" New topic
Author

init methods of Servlets: need clarification

Sreeraj G Harilal
Ranch Hand

Joined: Apr 19, 2006
Posts: 310
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.


SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
init()


Jigar Naik


Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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 ]

Turn on, tune in, drop out.
Sreeraj G Harilal
Ranch Hand

Joined: Apr 19, 2006
Posts: 310
Thank you Sayak.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572

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

Joined: Nov 28, 2006
Posts: 292
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

Joined: Mar 18, 2004
Posts: 572
But these specs doesn't say that super.init(ServletConfig) call is mandatory while overriding init(ServletConfig).

Am I right?
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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

Joined: Mar 18, 2004
Posts: 572
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

Joined: Nov 28, 2006
Posts: 292
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

Joined: Mar 18, 2004
Posts: 572

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

Joined: Nov 28, 2006
Posts: 292
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.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
this is all is a bit confusing..

hmmm...need to brush up a bit !


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
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 ]

Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init methods of Servlets: need clarification
 
Similar Threads
Servlet Config
servlet context
why init() method is not final?
init() and init(conf)
call super.init(config)...