This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet constructor" Watch "Servlet constructor" New topic
Author

Servlet constructor

karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Hi all,

1.Can we write a customized constructor in a servlet ?
2.How container calls the constructor ?


karim
Hany Shafik
Ranch Hand

Joined: Jun 21, 2008
Posts: 80
What do you mean by having customized constructor ?
Do you mean to declare a constructor that have parameters?
If yes then it is illegal to declare such Servlet. Actually the use of constructors instead of the init() method in Servlets is discouraged.
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
I mean any constructor with or with out parameters !
Servlets is also a java class then why can't we write a constructor for that ?
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

Hi karimkhan,
Servlet 2.4 final release specification wrote:
SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for loading and instantiating servlets. The loading
and instantiation can occur when the container is started, or delayed until the
container determines the servlet is needed to service a request.
When the servlet engine is started, needed servlet classes must be located by
the servlet container. The servlet container loads the servlet class using normal
Java class loading facilities. The loading may be from a local file system, a remote
file system, or other network services.
After loading the Servlet class, the container instantiates it for use.

SRV.2.3.2 Initialization
After the servlet object is instantiated, the container must initialize the servlet before
it can handle requests from clients. Initialization is provided so that a servlet can
read persistent configuration data, initialize costly resources (such as JDBC™ APIbased
connections), and perform other one-time activities. The container initializes
the servlet instance by calling the init method of the Servlet interface with a
unique (per servlet declaration) object implementing the ServletConfig interface.
This configuration object allows the servlet to access name-value initialization
parameters from theWeb application’s configuration information. The configuration
object also gives the servlet access to an object (implementing the ServletContext
interface) that describes the servlet’s runtime environment. See Chapter SRV.3,
“Servlet Context” for more information about the ServletContext interface.



SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
Do you mean to declare a constructor that have parameters? If yes then it is illegal to declare such Servlet.

No, it's perfectly valid to have constructors with arguments in a servlet, as long as there's also a no-argument constructor. Of course, there's no point in doing so, since that constructor will never be called by the servlet container.


Ping & DNS - my free Android networking tools app
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Thanks,

If I have written a parametrised constructor in the servlet ,then there will be no default constructor provided by the JVM ..then how servlet wil run.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
There would need to be an explicit no-argument constructor.
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Thanks ,

We can write a constructor of parameters when there is a no- arg constructor only.
The container will only call the no-arg constructor

does these statements are correct ?
if I'm wrong correct me ..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
That's correct. And having said that, servlets should never have *any* explicit constructors. All initialization should be done in the init methods.
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Thanks Ulf that was helpful ..
 
GeeCON Prague 2014
 
subject: Servlet constructor