File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes servlet costructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet costructor" Watch "servlet costructor" New topic

servlet costructor

ravi inguva
Ranch Hand

Joined: Feb 07, 2002
Posts: 34
What happens if v write a constructor for a servlet
will it get executed??

thank u in advance
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
Hello ravi

The term constructor does not relate to a servlet. Basically a constructor for a particular class is used to initialize some variables or do process which is necessary when an object for that particular class is created.
In case of servlets its not so.. when you access a servlet, a thread is created for that particular request which does the necessary processing .. its not like instantiating an object.
And one more thing just in case you want that certain parameters be initialized or whatever , when the servlet is first loaded in the memory or the servlet is first called , then use the init() method for that purpose.
by the way whatever code you write for the servlets constructor will get executed that too only once .. ( i guess ) when the servlet is first called. better confirm it
i hope this helps
[ March 04, 2002: Message edited by: Dharmesh Chheda ]

regards,<br />Dharmesh Chheda
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Naturally your custom servlet class will have a constructor that will be called in the normal way when the servlet engine needs an instance. This will be either on startup or when first requested. The servlet engine expects a no-args constructor.
It is bad practice to use a custom constructor with servlets. As Dharmesh said, the servlet engine is NOT going to be building an instance for every request and you are expected to use the init method for any initialization.
I agree. Here's the link:
subject: servlet costructor
It's not a secret anymore!