GeeCON Prague 2014*
The moose likes Servlets and the fly likes Why dont we write constructors in servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Why dont we write constructors in servlets?" Watch "Why dont we write constructors in servlets?" New topic
Author

Why dont we write constructors in servlets?

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Yes, As paul mentioned, servlet must have default constructor[no-arg constructor] .
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
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

Joined: Jan 28, 2008
Posts: 5575

did you read the link which is given by paul ?
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why dont we write constructors in servlets?