• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init and destroy method

 
felix thomas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i call destroy method explicity from init method.
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never tried it, but I suspect an exception will be thrown since the servlet would not successfully initialize.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's just a method so you can call it.
The only significance of the name destroy is that, given the correct signature, it will also be called during garbage collection.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually not called during garbage collection, but during server shutdown -- but other than that, Jeroen's absolutely right -- it's just another method.

Kyle
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, an execption will be thrown as I suspected:



It is a method, but not any method.
 
felix thomas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code and i don't get an exception.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Inittesting extends HttpServlet
{
PrintWriter out=null;


public void init(ServletConfig s) throws ServletException
{

super.init(s);
System.out.println("in init before");
super.destroy();
System.out.println("in init");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{


try
{
//common instantiations required for the document login to build up
out=response.getWriter();

response.setContentType("text/html");

//super.destroy();
out.println("servlet now 33 ");

}catch(Exception e)
{
out.println("exception :"+e);

}
}



}
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does on Websphere:

 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Thomas, can you show your initialization output?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yah, was confused with finalize()...
Too much reading about garbage collectors for my SCJP study lately
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
yah, was confused with finalize()...
Too much reading about garbage collectors for my SCJP study lately


Maybe a good time for a cold beer (an extra cold Guinness stout for me) and some good sleep.
Been there...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic