constructor in servlets
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, February 7, 2006 08:51 AM

Can we have our own construtctors in a servlet?
I tried it .
Some times it is working fine,but sometimes it doesn't.
Can any one tell me clearly?

Re: constructor in servlets

Posted By:   guru_prasanth  
Posted On:   Tuesday, February 14, 2006 02:23 AM

u can create a constuctor in servlet but u should not intialise servletcontext object in the constructor u have created becuase servletcontext is being intialised by your appserver.

Re: constructor in servlets

Posted By:   Anonymous  
Posted On:   Friday, February 10, 2006 02:40 AM

We can have constructors in servlet. We know the servlet life cycle is in the manner init() --> service() --> destroy(). In the init(), we get the servlet initialisation paramters through servlet config. But if we try to manipulate it in constructor of the servlet, the servlet config object may not have been initialised by the time the constructor is called (when servlet is instantiated). So our purpose of initialising servlet init parameters may not work properly with constructors.

Re: constructor in servlets

Posted By:   Raghavender_Bandar  
Posted On:   Thursday, February 9, 2006 04:44 AM

we can write constructors in servlets but no use y b'cause we are not creating object for servlet ,if u writing any parametrised constructor then we have to write default constructor also y b'cause default constructor will provide by jvm automatically whenever we did't write any constrctor but if we r writing only parametarised constructor then jvm will be deactivated to provide default constructor then we will get error

Re: constructor in servlets

Posted By:   ankush_purwar  
Posted On:   Wednesday, February 8, 2006 01:22 PM

I have never seen the consructor in servlet. but it doesn't mean that you can't write a constructor in servlet.
But i am not able to understand why you are defining constructor in servlet. See the life cycle of servlet
init()
service()
service()
service()
.
.
.
.
.
.
destroy()

you can do the same thing in init() rather in constructor.
Then why you are defining a constructor.Even you will never create the object of servlet, it's completely depends on web-container when to create, how to create etc..

Hope you understands now.
About | Sitemap | Contact