Can we have a public default constructor for servlet ?

Showing Answers 1 - 7 of 7 Answers

Vivek

  • Aug 30th, 2006
 

We have only init() method in servlet which will be called when the servlet is loaded for the first time. There is nothing like public constructor we have here, init() method itself will act like constructor.

ramana

  • Aug 31st, 2006
 

       When client send request to the servet then container identify the servlet in web.xml file and create instance for the servlet by calling default constructor.if u insert default and any argumented contructor in your servlet then container calls default constructor only whether is is public or private.if u give public for constructor then we crete object for servlet in outside the class but no use of this type,because no one can receive your object.i.e servlet is managed by container not with jvm.

  Was this answer useful?  Yes

mohit bahl

  • Sep 4th, 2006
 

Like any other java class we can also declare constuctor in a servlet. the container when instantiates a servlet it calls its default constructor. so in case you are planning to declare a constructor make sure it is a non-parameterized constructor which will be then called by the servlet container. But if you declare a parameterized constructor and no non-parameterized constructor is declared then the servlet container will throw an error as "Error 500: Failed to load target servlet [StartingServlet] "cheers!

  Was this answer useful?  Yes

sureshkumar M

  • Sep 9th, 2006
 

Thank u vivek i got a information from GeekInterview.com keep in touch with me

  Was this answer useful?  Yes

vidya

  • Sep 15th, 2006
 

Yes , we can have a default concstructor for servlet class . If not it adds a zero argument constructor to the class file when it compiles.

  Was this answer useful?  Yes

karthik chowdary

  • Sep 20th, 2006
 

There is no meed to create another  default constructor.init() method   will  act  as a constructor.

  Was this answer useful?  Yes

Priya

  • Oct 19th, 2006
 

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.

  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