GeeCON Prague 2014*
The moose likes Servlets and the fly likes regarding constructor in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "regarding constructor in servlet" Watch "regarding constructor in servlet" New topic
Author

regarding constructor in servlet

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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

Joined: Aug 04, 2008
Posts: 57
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
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
You should never put a constructor into a servlet. Any initialization you need to do should be done in one of the init methods.


Ping & DNS - my free Android networking tools app
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

This FAQ entry may help !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Shahnawaz Shakil:
But instance variables itself in servlets are discouraged.


Why ? Whats the big deal ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
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

Joined: Aug 04, 2008
Posts: 57
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

Joined: Apr 29, 2008
Posts: 234
thanks everyone
 
GeeCON Prague 2014
 
subject: regarding constructor in servlet