Can we call destroy() method inside the init() method? What
happens when we do so?
Answer Posted / 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 |
Post New Answer View All Answers
What exactly is a servlet?
What is ServletContext object?
What are the different mode that servlets can be used?
Define context initialization parameters.
What is the use of welcome-file-list?
Is servlet thread safe?
What are the various ways of session supervision in servlets?
What is the difference between the include() and forward() methods?
What is a servlet context?
What is cgi and what are its drawbacks?
Whether thread can be used in servlets?
How can you create a session in servlet?
When is the servlet instance created in the life cycle of servlet? What is the importance of configuring a servlet?
How does java thread pool work?
What is the difference between 2 types of servlets?