Why init is used and not constructor for servlets initialization?

Questions by javadeep   answers by javadeep

Showing Answers 1 - 4 of 4 Answers

Naveen Malik

  • Jul 8th, 2006
 

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

Ramu Kondapalli

  • Jul 19th, 2006
 

Servlet life cycle is mannaged by the servlet container not the JVM.

According to servlet specification the servlet container calls init() method of servlet, not the servlet constractors. At this point the constructor is called the class is not really a Servlet.

Because the servlet container manages the servlet lifecycle, one should never call the constructor, the init and destroy methods.

  Was this answer useful?  Yes

sunil

  • Aug 10th, 2006
 

If u used a constuctor then no probs it's working fine but u cant used servletconfig and u cant passed argument constructor b'coz only first time yr constructor has called and for other request directlly service method called  so we cant able to use  initilization paramter for that reason we r avoid 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