wood burning stoves 2.0
The moose likes Servlets and the fly likes Servlet and Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet and Constructor" Watch "Servlet and Constructor" New topic

Servlet and Constructor

umasankar puranam
Ranch Hand

Joined: Sep 24, 2004
Posts: 31
Its obvious that a servlet is a class. Can we write a constructor in a class which extends from HttpServlet? Please answer

umasankar<br />Software Engineer
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980
Hi Uma,

Thats a good question...Good Going....

Richard Bradford
Ranch Hand

Joined: Apr 20, 2004
Posts: 48
Why not.
All classes need a constructor. If you don't write one a default constructor will be provided.
Santosh Jagtap

Joined: Sep 20, 2004
Posts: 23
Yes you can definately write constructor in ure servlet class it will be executed when your servlet gets loaded.


Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yeah you can. but again its mendatory to ask why?? why you want to do that??

please dont say tht you are going to instantiate your servet inside some of your normal java class. although you can instantiate your servlet with a default constructor. and if it is like that it is very bad idea.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
therefore remember that the only constructor that may make any sense to create is a no-argument constructor as that's the only one that the servlet engine will expect there to be and thus call.

But of course the correct place for servlet initialisation code is inside the servlet's init method, not the constructor!

And please don't think to do such things as opening global database connections and files in servlets!
At most you could use it to load some static data like global constants but a static initialisation block is probably more suitable to that.

I agree. Here's the link: http://aspose.com/file-tools
subject: Servlet and Constructor
It's not a secret anymore!