473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

destroy() method of Servlet

anurag275125
Hello all,

can anyone please acquaint me what is the purpose of destroy() method in servlet life cycle. I found many answers regarding this, but I'm not satisfied with these answers. In a book I read that "when the web container determines that a servlet should be removed from the service, it calls the destroy() method to allow the servlet to release any resources it is using. The common use of destroy() method is to close the database connection".

I write a simple servlet to test the servlet life cycle like this:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class LifeCycleServlet extends HttpServlet
  5. {
  6.     public void init() throws ServletException
  7.     {    
  8.         System.out.println("In init");
  9.     }
  10.     public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  11.     {
  12.         System.out.println("In service");
  13.         PrintWriter pw=response.getWriter();
  14.         pw.println("Hello from LifeCycleServlet");
  15.     }
  16.     public void destroy()
  17.     {
  18.         System.out.println("In destroy");    
  19.     }    
  20.  
After starting the Tomcat server. When I run the servlet for the first time. It displays "In init", and "In service" message on the console. But it doesn't display the "In destroy" message. After that whenever I run the servlet It displays "In service" message. "In destroy" message is displayed when I shuts down the server. We can say that the servlet life cycle ends when we shuts down the server. So what's the actual use of destroy method?????

Please help me out.....
Mar 19 '10 #1
2 3591
jkmyoung
2,057 Expert 2GB
With your basic example, it doesn't do anything really. Your service isn't complicated enough to be requesting resources from other programs. It doesn't open any file, call any other program or service. Most non-trivial services actually do things, so they have a need for a destroy method to release requested resources.
Mar 19 '10 #2
If we create connection to the database in the init method, and we're releasing the connection in destroy method. Then in this case, with each client request, is the connection is established in init method, request is processed in service method and finally, destroy method is called to release the connection??
Mar 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Flavio | last post by:
Hi, I have a miniframe composed mainly of combo boxes, that I need to destroy and recreate multiple time with different choice lists for the combo boxes. My problem is that even after...
1
by: emresyus | last post by:
I have developed a web application with servlets. Application aims to control and deploy new applications based on thread technology. It works like tomcat web application manager but in my...
2
dmjpro
by: dmjpro | last post by:
we know all the things related to servlet lifecyle but this is enough for say any one ... but in practical it needs quite clear conception... now listen my conception .... A servlet is loaded...
1
by: Ramanjaneyulu | last post by:
Hi, Can we write the destroy() method in our service () method in servlets? Give brief example? Thanks in advance.
2
by: Rajendran Appavu | last post by:
When I am done with a widget that is packed in a Frame, is it safe to call destroy() method on the widget after calling its pack_forget() or grid_forget() method? -- Thanks, Rajendran.
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
6
by: itsraghz | last post by:
Dear All, I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be...
2
anurag275125
by: anurag275125 | last post by:
Hello all, Can anyone please tell, is it possible to override the init() and destroy() methods of Servlet in JSP page. When I override the init() method then it gives no error, but when I override...
2
by: jerald m | last post by:
Hi, For Login authentication. i create JSP Form. login.jsp(webapps/loginsystem/login.jsp) <form method="POST" action ="http://localhost:8085/loginsystem/lo" name="login"> <font...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.