Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet costructor

 
ravi inguva
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if v write a constructor for a servlet
will it get executed??

thank u in advance
 
Dharmesh Chheda
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
bye
[ March 04, 2002: Message edited by: Dharmesh Chheda ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic