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

Showing Answers 1 - 21 of 21 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.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

varun

  • 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

  Was this answer useful?  Yes

mannam Haribabu chowdary

  • 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.  

  Was this answer useful?  Yes

mukesh pandey

  • 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

  Was this answer useful?  Yes

Praveen D

  • Feb 1st, 2013
 

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

  Was this answer useful?  Yes

rekha

  • 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 doesn’t get performed. As a result getServletConfig() will return null.


  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