Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Why dont we write constructors in servlets?

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am quite interested in knowing why we dont write any constructors in servlets!
Can we write constructors in servlets?
Whats the fundamental involved here?

Regards,
Sriram
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We never construct Servlets, that is always done by the Servlet container. Have a read through this, it should explain a lot of the background.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, As paul mentioned, servlet must have default constructor[no-arg constructor] .
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me take the older type of servlets that developers write.
Developers write with out.println() statements to print the html.
In such cases also, we dont write constructors.
Its fine that default constructor is available for any class.
But, is there any specific reason for not writing constructor in such cases?

Regards,
Sriram
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you read the link which is given by paul ?
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there's no stopping anyone from writing a constructor for a servlet. However, you need to keep in mind that the servlet context is NOT initialised at the point when the constructor is invoked.
You can most definitely perform some house-keeping tasks like initialisations or checking on the availability of services/resources, but that's the very purpose for which the init() is available as part of the servlet's lifecycle.

In my opinion, there's no particular 'purpose' to defining a constructor for a servlet. Of course, one might never know when a skewed requirement comes along mandating a constructor definition for a servlet. Do let us know if you're already aware of any such requirement.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic