Can we define constructors in Servlet?

Answers were Sorted based on User's Feedback



Can we define constructors in Servlet?..

Answer / vivekanand prasad

The init() method creates and loads the servlet.But the servlet instance is first created through the constructor (done by Servlet container). We cannot write constructors of a servlet class with arguments in servlet (It will throw Exception). So, They provided a init() method that accepts an ServletConfig object as an argument.ServletConfig object supplies a servlet with information about its initialization (init) parameters.Servlet class cannot declare a constructor with ServletConfig object as a argument and cannot access ServletConfig object.

Is This Answer Correct ?    20 Yes 3 No

Can we define constructors in Servlet?..

Answer / qim2010

One can definitely have constructor in servlet. Even you can
use the constructor in servlet for initialization purpose,
but this type of approach is not so common. You can perform
common operations with the constructor as you normally do.
The only thing is that you cannot call that constructor
explicitly by the new keyword as we normally do. In the case
of servlet, servlet container is responsible for
instantiating the servlet, so the constructor is also called
by servlet container only.

Is This Answer Correct ?    1 Yes 0 No

Can we define constructors in Servlet?..

Answer / sitaram

Each every class execute in java. One default constructor or
define constructor must be calling. If you are not any
constructor write in your class. when execute the program ,
JVM automatically create the default constructor.

Is This Answer Correct ?    3 Yes 5 No

Post New Answer

More Advanced Java Interview Questions

What is servlet preinitialization

1 Answers   iFlex,


In a multitiered application which tier is the browser in?

10 Answers   Adobe,


What state does a thread enter when it terminates its processing?

0 Answers  


What is the argument type of a programs main() method?

0 Answers  


How task's priority is used in scheduling?

0 Answers   HCL,






Explain the difference between object state and behavior?

0 Answers  


what is the Remote Stub?

1 Answers  


If we opened windows notepad 4 times, does it starts 4 processes or 4 threads?

1 Answers  


difference between applicationserver and webserver

4 Answers   SolutionNET,


What is the map interface?

0 Answers  


What is the difference between a stub and a skeleton?

9 Answers   ABC, College School Exams Tests, Geoservices, Polaris, Qualcomm, Rajiv Gandhi College of Engineering and Technology RGCET, UTIITSL, Wipro,


String is immutable.it is true String can be change to mutable How?

6 Answers   IBM, Professional Access, TCS,


Categories