ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Java Related >> Java J2EE >> Servlets
 
 


 

 
 Core Java interview questions  Core Java Interview Questions (1721)
 Advanced Java interview questions  Advanced Java Interview Questions (331)
 Swing interview questions  Swing Interview Questions (44)
 EJB interview questions  EJB Interview Questions (134)
 Servlets interview questions  Servlets Interview Questions (255)
 Struts interview questions  Struts Interview Questions (196)
 JDBC interview questions  JDBC Interview Questions (187)
 JMS interview questions  JMS Interview Questions (9)
 SunOne interview questions  SunOne Interview Questions (3)
 J2EE interview questions  J2EE Interview Questions (71)
 Weblogic interview questions  Weblogic Interview Questions (146)
 Websphere interview questions  Websphere Interview Questions (14)
 Java Networking interview questions  Java Networking Interview Questions (20)
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions (87)
Question
Can we call destroy() method inside the init() method? What
happens when we do so?
 Question Submitted By :: Servlets
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
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 ?    19 Yes 4 No
Mani
 
Answer
# 2
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 ?    7 Yes 3 No
Suraj Kumar
 
 
 
Answer
# 3
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 ?    5 Yes 2 No
Ashwin Kumar J
 
Answer
# 4
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 0 No
Amitasite
 
Answer
# 5
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 9 No
Poorna Chandar Rao.yerrabothu
 
Answer
# 6
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
Ravikiran
 
Answer
# 7
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
Ravikiran
 
Answer
# 8
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
Srilatha
 
Answer
# 9
Yes you can call init method in destroy.
public void destroy(){
try {
init();
}
catch(Exception e){

}
}
 
Is This Answer Correct ?    11 Yes 25 No
Sunny
 

 
 
 
Other Servlets Interview Questions
 
  Question Asked @ Answers
 
java.lang.IllegalArgumentException: The path of an ForwardConfig cannot be null while working with struts it displayed pls very ungent IBM 3
why business logic written using servlets not in jsp. Jsp used for presentation purpose. serlvet used for coding business logic and controller logic. Reason for using servlets in business logic.   4
Hi Friends, Suppose you have a web appliction which consists of 100 clients are connected with Connection pooling and they are associated with 100 connection objcet and connected with DB sever,Due some problem DB-Server suddenly crashed and server came to down state for a while after few minutes DB-server once again up,Now how will I manage my Pool Management with my 100 clients...can any one tell me?????????????????? ProgressSoft 3
What is context switching?   4
What happens, when client requests for server object, which is not yet loaded into the memory?   2
Hi............ When i am started the tomcat server it started. but it not load the jar files wht we deploving file.suppose we load the wapp folder in webapps. At running time it show the below error in tomcat server INFO: validateJarFile(C:\Program Files\Apache Software Foundation\Tomcat 5.0\web apps\wapp\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, s ection 9.7.2. Offending class: javax/servlet/Servlet.class How to slove this problem in my system. plz give answer for this problem.   5
What is the capacity that doGet method can send to the server?   5
which method in doGet or doPost is use to send binary date to server HCL 2
What is the difference between an applet and a servlet? Miracle-Solutions 15
What is HTTP Session ?   6
how to pass parameter from servlet to html form   4
what are binding listners?   1
 
For more Servlets Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com