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

Showing Answers 1 - 33 of 33 Answers

veeru

  • Sep 8th, 2005
 

For Instatiating a class we are using constructors. 
 
But in servlet init() method itself acts as constructor,here initialisation of servlet will takes place.

  Was this answer useful?  Yes

Vj

  • Sep 15th, 2005
 

Yes, We can write our own constructor  instead of init() n we also have to pass servlet configuration in it [servlet config() ]

  Was this answer useful?  Yes

savita

  • Oct 21st, 2005
 

we can't pass object of servlet config in the constructor.thats why we use only init() method

  Was this answer useful?  Yes

ravi

  • Feb 15th, 2006
 

Syntactically, we can a write a constructor in a servlet. But if u write parameterized constructors ensure that u also add zero-arg constructor.If u dont add a zero-arg const. servlet instantiation fails. Infact, a constructor doesnt serve any purpose in a servlet except for the zero-arg const. which is used at the time of instantiation of the servlet object.

  Was this answer useful?  Yes

Lavanya

  • Mar 16th, 2006
 

Well, the primary idea of a servlet is 'it has to be singleton'. Ofcourse we can have a flavor of singleton where we could create a fixed number of instances. Have you seen constructors in singleton? ;-)

  Was this answer useful?  Yes

Yes , of course you can use the constructor instead of init().There?s nothing to stop you. But you shouldn?t. The original reason for init() was that ancient versions of Java couldn?t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won?t have access to a ServletConfig or ServletContext.Yes, We can write our own constructor  instead of init() n we also have to pass servlet configuration in it (servlet config)

K. Venkat Reddy

  • Aug 13th, 2011
 

The object of servlet can not be created by the programmer/developer. When the enduser is accessing the web at that situation the object of servlet will be created automatically by the container. If any initializations are needed that will be done by the init() method.

So there is no need of servlet. And we cannot write a constructor for the servlet.

  Was this answer useful?  Yes

BoRn

  • Aug 28th, 2011
 

We can write a constructor in Servlet, but it should be default constructor. Because Servlet internally uses Class.foName() which cannot create a class to parametrized constructor.

  Was this answer useful?  Yes

mohit

  • Sep 13th, 2011
 

can we create the constructor in servlet ? how

  Was this answer useful?  Yes

Falcon

  • Sep 19th, 2011
 

There can be constructors for servlets. Though the life cycle method init() provides the functionality of a constructor, its still a good programming practice to include constructors because the container still uses the constructor to create an instance of the servlet

  Was this answer useful?  Yes

suraj20p

  • Oct 28th, 2011
 

Servlet is a java class,whose object will be created by servlet container,and then container will call 3 life cycle method on that object.

container creates our servlet class object using 0-args constructor,If u want to place your own constructor you can,but make sure 0-args constructor is always present.

However,our parameterised constructor will not be used by servlet container,and will be of no use.

Also If u declare your own 0-args constructor,make sure it is public,so that it could be easily accessible to container

  Was this answer useful?  Yes

webdynamics

  • Oct 31st, 2011
 

Yes, you can write a constructor in your Servlet if required. But you must understand how the sequence of flows.

1. Servlet instance is created i.e. constructor is called. Note: ServletConfig is not available.
2. Servlet container calls init(ServletConfig config) and injects ServletConfig instance in it.
3. Servlet container calls init(). Note: ServletConfig is available.

You may want to use init() for your init tasks since ServletConfig will be initialized by this time.

  Was this answer useful?  Yes

Rohit

  • Jul 30th, 2012
 

do get is like a open store, let come take what you want...but match the address....then
dopost is posting the all data to where is a address like all information in one box.

  Was this answer useful?  Yes

sujata waghmare

  • Jan 10th, 2014
 

yes..we can write a constructor in servlet...most important things is init() only inilization ....but constructor perform operation thats why we can write a constructor in servlet...

  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