File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init()" Watch "init()" New topic


Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226

I'm still new to Servlets etc. so please bear with me!

Reading up about them and the beginning of their lives, I noticed that there are two init() methods -
one which takes no args
one which has a ServletConfig object.

I believe that the idea is that we, as developers, use the no args one to do any of the initialisation we want.

I then wondered which order these were called by the container, as the book I'm reading (HFSJ p97) mentions init() but not init(ServletConfig).

I've editted the earlier example in the book as a test so it now reads:

And Tomcat 5.5 running from the commandline gave me:

This shows to me that my overridden init() was called before init(ServletConfig).

Does this mean that in my init() I am unable to access the servletConfig?
Doesnt that go against the whole point of the init method?

Thanks in advance,


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

Have a look at the question "What is the difference between the init() and init(ServletConfig) methods?" at ServletsFaq. See if it helps.

[My Blog] [JavaRanch Journal]
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
It does. Thanks very much.

I assumed these methods would be called indivudally by the container. I didnt realise that one called the other.

To prove the case, I reordered the System out's so that they print when the method is entered, not when it leaves...


Also proves that in the init() method with no arguments (that the developer is encouraged to override) you DO have a handle on the ServletConfig.

Thank you Jaikiran for your very quick response. :-)

[ January 08, 2007: Message edited by: Mark Garland ]
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
HFSJ p151 mentions that "The inherited init(ServletConfig) method calls the no-arg init() method, so the only one you need to override is the no-arg version".

Guess it was just me thinking-ahead and being impatient!
[ January 09, 2007: Message edited by: Mark Garland ]
I agree. Here's the link:
subject: init()
It's not a secret anymore!