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
Write a hello world program using servlets.
What is the difference between context parameter and context attribute?
Explain the architechure of a servlet?
Is java servlet still used?
What is context in servlet?
Tell us something about servletconfig interface.
How do you run a servlet?
How a servlet is unloaded?
List out the difference between ServletConfig and ServletContext?
When should you prefer to use doget() over dopost()?
What is servlet in simple terms?
Differentiate between the get and post method
Which protocol will be used by browser and servlet to communicate
Explain the difference between jsp and servlet?
What do you mean by request dispatcher in servlet? Also explain its methods.