Why there is no constructor in Servlet?

Servlet is Java class. Then why there is no constructor in Servlet? Can we write the constructor in Servlet

Showing Answers 1 - 25 of 25 Answers

Tushar

  • Apr 13th, 2006
 

It is a java class only but but it does not support Constructor becuase Swrvlet is loaded by servlet container not through normally we initilized the java class

like X x = new X();

  Was this answer useful?  Yes

Pankaj

  • Apr 13th, 2006
 

If there is no constructore in the Servlet , then it does not mean that we can not write the constructore in the servlet. Offcourse we can write. As we know that the constructore only instantiate the normal java class and servlet is not a notmal java class. To make a object as servlet, a lot of properties has to be defined. and this property comes by init() method. so, writing the constructore in servlet is worthless.

  Was this answer useful?  Yes

uafshahid

  • Apr 30th, 2006
 

Servlet is a special type of java class. it has init() method which works like a constructor and understood by the servlet container.

  Was this answer useful?  Yes

First of all Servlet is not a Java class. Its an interface, interface does not have constructors. And java servlet container implents servlet creating respective servlet classes which are mapped in deployment discriptor.

init method performs the task on normal constructor in servlets as its called by the actual class taht implements servlet in the container run time by calling the init method.

  Was this answer useful?  Yes

Simon

  • May 16th, 2006
 

Yes a servlet is a java class and i dont agree that it dosent have a constructor(dont mistake this statement..). It is a special java class... think of it as an applet... unless u are planning to add the applet to a JFrame and the running it explicitly i dont recall creating an applet object... this is because the applet has its own life cycle.. and so does the servlet .... and even when the container loads the servlet into memory... ie first the servlet class is loaded... the constructor is called... (till now the servlet object is not a servlet but a normal java object...)... it becomes a mighty servlet when it is given the servletContext and the ServletConfig.. so basically the constructor of the servlet is too eary in its life cycle to do initialization... hence the init() method comes to the rescue.. just like the init() of the applet.... and the implementation is mostly vendor specific so the various interfaces in javax.servlet.http.* are implemented by the Container and the container inturn calls the constructors when a reference to the base interface is requested...RegardsSimon

  Was this answer useful?  Yes

Raja vardhan Reddy

  • Jul 19th, 2006
 

Every java class will have aleast one constructor and servlets are no exception to this.

But while using servlets ,we never instantiate the servlet rather the container does it for us by using the newInstance() defined in the Class class;  which in turn uses the empty(Default) constructor to create a new instance of the servlet.

If we define parameterised constructor in the serlet ,then container will fail in instantiating the servlet.

The rest is left for you to decide whether a servlets need a user defined constructor or not.

dsmanyam

  • Jul 29th, 2007
 

Yes, a servlet is like any other JAVA class, the anamoly being that it implements the Servlet Interface indirectly which provides the template for the servlet lifecycle which is implemented by the underlying Container.

Now if I have a constructor for a servlet, the basic issue is that every time a new request comes in the constructor of the servlet is called upon and thats not what we would want to happen. Thats the reason why a member method is being used as a entry-point to a servlet so that the container takes care that the init() method is called only once during the lifecycle of the servlet.

keep coding and keep testing

  Was this answer useful?  Yes

Kasi

  • Aug 30th, 2007
 

Hi,
  It is correct that each and every implementation of servlet interface will have the default constructor, like any other java class and will be used by the servlet container to instantiate it.
Different steps involved for using a servlet by servlet container for handling requests:
1. Load the servlet class.
2. Instantiate it ( Default constructor will be called).
3. Initialize it( init() method will be invoked).
4. Handle the requests (service() method will be invoked).
5. Destroy the servlet (destroy() method will be invoked).


Regards,
Kasi.

  Was this answer useful?  Yes

-->Servlet is Java class
Wrong, Servlet is an interface.

-->Why there is no constructor in Servlet?
Interfaces don't have constructors.

-->Can we write the constructor in a Servlet?
You can create a servlet by implementing the servlet interface. However your would have to override its abstract methods and then probably you can define your own interface.

  Was this answer useful?  Yes

sandyavs

  • Jan 4th, 2008
 

why the servlet is an interface? it's super classes are interfaces? we are overriding and implementing the methods which are defined in the superclasses?
Is the servlet class or interface?

  Was this answer useful?  Yes

san.manish

  • Apr 8th, 2009
 

A servlet is just like an applet in the respect that it has an init() method that acts as a constrcutor. Since the servlet environment takes care of instantiating the servlet, an explicit constructor is not needed. Any initialization code you need to run should be placed in the init() method since it gets called when the servlet is first loaded by the servlet container.

  Was this answer useful?  Yes

jontyjonty

  • Sep 10th, 2009
 

We can have parameterized constructors for servlet.

Explanation:
Consider this example

class One implements Servlet{}

-> now One is a java class having all the special features of a servlet.
-> so just as for any class, we can have a parameterized constructor for our One class, which is a servlet (and also a java class)

but the important thing is that we are not instanceating the servlet (here One class)
our server is instanceating it, and for that it is using "constructor with 0 parameters"
so now come to basics, we are
not providing any constructor for One class, so default constructor is provided to our class One.
but if we write parameterized constructor in our class
One (which is also a special class called servlet), then compiler does not provide any default constructor. So we will get run time Exception.
to solve this you also write a zero argument constructor along with paramaterized constructor in your servlet (i.e our class One).
Some thing like this

class One implements Servlet{

public One(){}
public One(int x){}
-----
----
-----
}

Note:-although you can provide parameterized constructor in this way, it is of no use of having it, because it is never used.
Since servlet container always instanceates the servlets and does it only with zero aurg constructor.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions