What is the servlet life cycle?

Each servlet has the same life cycle:A server loads and initializes the servlet (init())The servlet handles zero or more client requests (service())The server removes the servlet (destroy()) (some servers do this step only when they shut down)

Showing Answers 1 - 4 of 4 Answers

Raja Mohamed

  • Jul 16th, 2005
 

In Servlet life cycles are, 
init(),services(),destory(). 
 
If init() executed at the time of servlet class loading.And init() executed only for first user. 
 
If services() are both for get and post methods. 
 
So if u want to use post method in html page,we use doPost() or services() in servlet class. 
 
if want to use get methods in html page,we can use doGet() or services() in servlet calss. 
 
Finally destory() is used to free the object. 

  Was this answer useful?  Yes

gokulakrishnan

  • Sep 19th, 2005
 

init(),service(),destroy() are the three methods we are using now .The init() is used to initialize the resources,the sevice() method is used to handle request & response,destroy() method is used to remove the resources from memory that is unused objects are recycled.

  Was this answer useful?  Yes

Santosh Kumar

  • Oct 13th, 2005
 

Servlet life cycle defines the life process of the servlet. It has certain methods that comes into existence throughout the utilization of the servlet. Generally newInstance() method creates an object to the class. Similar way, internally the newInstance method is called to create the servlet object. We will pass the configuration parameters to the init()method, which takes the parameter of ServletConfig object. This object is responsible for getting the necessary configuration details from the configuration file or the deployment descriptors i.e., web.xml and is responsible for initializing the servlet. This init() method is also capable of calling the servletcontext which contains the environment variable setup in it. so that it is capable of handling the global situations. Next method in the life cycle is the service() method where the actual tasks are performed. We will do the real coding in this method. Next comes the destroy() method which kills the servlet object.

  Was this answer useful?  Yes

Sonikanth

  • Feb 22nd, 2010
 

Servlet Life cycle contains public void init(ServletConfig cg)
public void service(HttpServletRequest req, HttpServletResponse res)
public void destroy()

init() with zero arguments is the helper method for init(-) with one parameter.

  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