Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Can we call destroy() method inside the init() method? What
happens when we do so?

Answers were Sorted based on User's Feedback



Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / mani

In the Servlet programming context, the destroy() method
can never be called by your code and is called by the
servlet container

Is This Answer Correct ?    23 Yes 6 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / ashwin kumar j

Yes you can call a destroy() method in init() method, but
remember destroy() method will behave like normal method
but not as life-cycle method.

Is This Answer Correct ?    9 Yes 3 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / suraj kumar

This action does not disturb the normal life cycle flow of the servlet. The servlet will work normal. You may test the below code.

/**
* @author Suraj
*
*/
public class TestServlet extends HttpServlet {


public void init(ServletConfig config)throws ServletException{
super.init(config);
destroy();
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

System.out.println("I am test servlet.");
}

}

Web.xml
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Servlet</display-name>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.esspl.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
------------------------------------
Test URL: http://<HOST>:<PORT>/<Context>/TestServlet.do

Is This Answer Correct ?    10 Yes 4 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / ravikiran

a call to the destroy method will happen and the resources
will be released and the flow will continue as it is and
will throw an exception if the released resource is used
indie the service method call.

Is This Answer Correct ?    2 Yes 0 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / amitasite

One can call destroy method from init method. One scenario
in which you would like to call is some resource is looked
up once in init() method and cleaned in destroy() method. If
exception is thrown before it finishes as servlet is not
brought in service, container won't call destroy method. so
to keep resource clean you can call destroy() method from init()

Is This Answer Correct ?    3 Yes 1 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / poorna chandar rao.yerrabothu

if suppose we take the servlet programming side then
whenever u type the URL first web browser call the service
method then create the servlet object then call the
init()method whenever we call the init method start the
proceesses of the particular object operation suppose we
call the destroy()method before completion of the particular
object operation servelet object is removed then when we r
call the service method then create the servelet object

Is This Answer Correct ?    11 Yes 10 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / ravikiran

a call to the destroy method will happen and the resources
will be released and the flow will continue as it is and
will throw an exception if the released resource is used
indie the service method call.

Is This Answer Correct ?    2 Yes 2 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / srilatha

In the servlet life cycle First our request calls to the
init()method,Inside it will doing some operations related
to the objects after that it will cal the destory() method
The destory()removes the servlet object,before calling to
service() method.

Is This Answer Correct ?    2 Yes 12 No

Can we call destroy() method inside the init() method? What happens when we do so?..

Answer / sunny

Yes you can call init method in destroy.
public void destroy(){
try {
init();
}
catch(Exception e){

}
}

Is This Answer Correct ?    11 Yes 25 No

Post New Answer

More Servlets Interview Questions

What is called Scriptlet?

0 Answers  


What is servlet api used for conneting database?

0 Answers  


What is the web server used for running the Servlets?

0 Answers  


What is http servlet?

0 Answers  


Hi Friends, Can we make any method thread safe withour synchronized keyword?

2 Answers  


Write a command to get actual path of a servlet to the server?

0 Answers  


Explain url encoding?

0 Answers  


What is the major difference between context parameter and context attribute?

0 Answers  


What happens, if server sends a cookie to a browser that doesn't supports cookies?

2 Answers  


How printwriter is different from servletoutputstream?

0 Answers  


What are the difference between session and cookies in servlet? Explain

0 Answers  


What are all the advantages of servlet over cgi?

0 Answers  


Categories