GeekInterview.com
Series: Subject: Topic:
Question: 48 of 124

Why init(), destroy() can't be overridden in servlets?

Asked by: Interview Candidate | Asked on: Jul 11th, 2007
Showing Answers 1 - 7 of 7 Answers

Why not? We can override these two methods also. We can override init() method for connecting to the database or some other resources. The init() method called by the container only once ,when the servlet has been loaded into the memory. You can override the destroy method to release the resources. When we stopped our server the destroy method will call by the container. But the service method will call each and every time the request came.

  
Login to rate this answer.

We can override the init() and destory() methods in servelets.
The init() method called by the container only once ,when the servlet has been loaded into the memory. We can override init() method for connecting to the database or some other resources.We can override the destroy method to release the resources.

  
Login to rate this answer.
varun

Answered On : Jul 31st, 2012

bcz init() and destroy() methods can call only once in the life-cycle of a servlet due to which we can make only one object during the session , thats why it is better than CGI bcz in that we have to call init() method every time when we request the servlet

  
Login to rate this answer.
mannam Haribabu chowdary

Answered On : Aug 27th, 2012

int() and destroy() methods are override in servlet class surly 100% no problum but int() is not a life cycle method .it is a convenenance method Here we write initilising logics like conncetion obj and statement objects and destroy() are also one time execution block when programmer raising an event i.e shut down server like that

Code
  1. abstract class Generic servley
  2. {
  3. public void inti(ServletConfig cg)
  4. {
  5. ----------------------------
  6. -----------------------
  7. init()
  8. ------------------------
  9. --------------------
  10. }
  11. servletconfig getServletConfig()
  12. {
  13. return cg;
  14. }
  15. destroy()
  16. {
  17. --------------
  18. }
  19. class Hello extends GenericServlet
  20. {
  21. init()
  22. {
  23. ---------------------------------
  24. --------------------------------//logic to write con,st objects
  25. --------------------------------
  26. }
  27. public void destroy()
  28. {
  29. ------------------------------// logic to write release the resources
  30. --------------------------
  31. }
  32.  

  
Login to rate this answer.
mukesh pandey

Answered On : Nov 4th, 2012

init() is possible to override () if you have initialization code like database connection or registering your self.
service() method look the request and response. Determine the Http method (get,post). Override not possible

  
Login to rate this answer.
Praveen D

Answered On : Feb 1st, 2013

It is possible to Override init() & destroy() methods in servelts

  
Login to rate this answer.
rekha

Answered On : Feb 13th, 2013

Can override init & destroy .
People used to make mistakes by overriding inti(ServletConfig).
Because if we override and forget to call super.inti(ServletConfig) then default actions doesnt get performed. As a result getServletConfig() will return null.


  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Core Java Interview Questions

EJB Interview Questions

Hibernate Interview Questions

Java Interview Questions

Java Patterns Interview Questions

JDBC Interview Questions

JMS Interview Questions

JSP Interview Questions

Java OOPS Interview Questions

Ruby Interview Questions

Servlets Interview Questions

Spring Interview Questions

Struts Interview Questions

Websphere Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.