wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init()" Watch "init()" New topic
Author

init()

Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
Hi,

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
and
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,

MG


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
Marshal

Joined: Jul 20, 2005
Posts: 9955
    
163

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...



Gives....



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. :-)

MG
[ 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 ]
 
 
subject: init()
 
Similar Threads
Tomcat init ValidatorPlugin and TilesPlugin twice -- why?
Regarding preconfigured tomcat 6.0.16.
Solved: Tomcat wont start from Eclipse - java.net.SocketException: select failed
TomCat and SSL cert
Struts2 with tomcat 6 unable to deploy war