• 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

one query regarding init() method

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain in detail below given statements..

"we use the constructor, instead of init(), to initialize servlet. But you will not get the servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn�t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won�t have access to a ServletConfig or ServletContext. "
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This statement misses the point. Interfaces are used in Java to force an implementing object to have certain methods. "init(ServletConfig)" is part of the Servlet interface; that's where I can get the ServletConfig object if I need it. But the Java compiler can't force a class to have a constructor with a particular signature; so that would be an inferior approach that would introduce an additional error scenario for the container to handle.

As an aside, HttpServlet's "init()" method wasn't added until a later version of the Servlet API as a convenience method for those cases where a ServletConfig object wasn't required.

Servlets aren't the only place that work this way. Applets, too, are constructed by a container, and then their "init" method is called to let the applet code do any initialization that's required.
[ August 13, 2008: Message edited by: Ulf Dittmer ]
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for providing this information :-)
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic