• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic info skipped  RSS feed

 
kunal vora
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it compulsory to override any method of servlet life cycle for successful compilation and execution of a particular servlet? Please reply in regards of both Servlet and HttpServlet
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of Servlet:

You have to override all the life-cylce methods viz. init(), service() and destroy().

In case of HttpServlet:

No need to override all but only the doXXX method that you are using in your client-side(JSP or HTML which will call this servlet), like doGet or doPost.This is because HttpServlet extends Generic servlet which already implements the life-cycle methods.The HttpServlet calls the service method when we call a doXXX method.


A servlet is meant to process a request.When you have a request, you have to provide a service or a doXXX method implementation in your servlet.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were wondering this, I would create a servlet that extends HttpServlet with no methods overridden.
I would try to compile it and then I would try to deploy it.
I would see what happens.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rao raghu:

You have to override all the life-cylce methods viz. init(), service() and destroy().


Are you sure about this?
Why do you think you need to override these?
Look at:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/GenericServlet.html

Are the life-cycle methods that you've mentioned declared as abstract?
[ January 19, 2007: Message edited by: Ben Souther ]
 
kunal vora
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya but if you use netbeans or eclipse and if you do not override any life cycle methods than also the servlet successfully runs.....
what is the reason behind it then?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, look at the API for the object you're extending.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServlet.html

If it implements the methods required by the Servlet interface and those methods are not declared abstract, then you do not need to override them.

Of course, if you will need to override something if you want your servlet to actually do something.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet is just an interface, so if you use that as the basis for your servlet you will need to implement (not override) all the lifecycle methods.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!