• 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()

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic