• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding constructor in servlet

 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the purpose of having user defined constructor in servlets?

Thanks in advance
[ August 13, 2008: Message edited by: Bear Bibeault ]
 
Shahnawaz Shakil
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardly any use of user defined constructor in servlet! Generally in the classes, other than servlet, constructors are used to initialize instance variables. But instance variables itself in servlets are discouraged. I don't see any situation where actually you will require a user defined constructor.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never put a constructor into a servlet. Any initialization you need to do should be done in one of the init methods.
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shahnawaz Shakil:
But instance variables itself in servlets are discouraged.


Why ? Whats the big deal ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency issues. Servlets are multithreaded, so unless instance variables are immutable they need to be protected from concurrent access. Since that's easy to forget or to get wrong, it's best to avoid mutable instance variables.
 
Shahnawaz Shakil
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sagar,
Following thread in this site will be helpful for you.

http://www.coderanch.com/t/365486/Servlets/java/having-instance-varibales-servlets
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic