• 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

Why init ( ) why not Constructor ??

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we have init method ..........
After all why can't we make use of servlet constructor for initilization ....


Regards
Abhishek
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question , Why init() why not construtor ??
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can't make use of the constructor because the container calls it and therefore you can't pass any parameters to the constructor. Also at the point the constructor is called the class is not really a Servlet because it doesn't have a reference to the ServletConfig, which provides all the initialisation parameters etc.

Having said that if you really don't need the reference to ServletConfig and you really wanted to then I guess you could do some initialisation in the constructor, but bear in mind it is considered bad practice because that is what Sun supplied the init(ServletConfig) method for.

HTH

Mat
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I should have also added that I think Bert and Kathy described this quite well in HFSJ at some point, probably at the point they talk about the Servlet Life Cycle.

Mat
 
Abhishek Jain
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankx Mat

I think I should start taking Tutions from U .......... :-)

But Mat I didnt got ur point saying
You can't make use of the constructor because the container calls it and therefore you can't pass any parameters to the constructor

Why cant a container call constructor having parameters

Regards
Abhishek
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As it is the container that manages a servlets lifecycle, you must define a generic way of working for all servlets.
You can't use the constructor because otherwise you would have to modify the container to tell him to instanciate this particular servlet.
Sebastien
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhishek,

Thanks for the complement
As Sebastien said, the container can't pass parameters to the constructor, because you would need to change the container, that is rewrite parts of Tomcat etc, and how would the container know what parameters to pass to the constructor.

If someone was very clever and had the spare time, they could work out a way, maybe through the DD that the container could work out parameters for a constructor, and pass them in when the servlet was created. But that is so far away from the current servlet specification, and would be very difficult to implement in a generic fashion. Much better at this stage to just remember that the coonstructor needs to take no parameters.

Mat
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic