Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init()

 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the question "What is the difference between the init() and init(ServletConfig) methods?" at ServletsFaq. See if it helps.
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic