aspose file tools*
The moose likes Servlets and the fly likes Destroy Method in servlet constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Destroy Method in servlet constructor" Watch "Destroy Method in servlet constructor" New topic
Author

Destroy Method in servlet constructor

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
Can I have a non-parameterized constructor in servlet?
Will the servlet be loaded and executed,if it does have a default constructor,I mean if I define a constructor with no parameters?

If I call destroy method of servlet in the user defined constructor,will it be loaded and executed?

Will it destroy the servlet instance by calling the destroy method in the constructor?
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
You can have a non-parameterized constructor in servlet.The servlet be loaded and executed.

If I call destroy method of servlet in the user defined constructor,will it be loaded and executed?

I tested the above logic in Tomcat 5.5.12 and the servlet is getting executed.

In the javadoc for public void destroy() of javax.servlet.Servlet it is mentioned
After the servlet container calls this method, it will not call the service method again on this servlet..
So may be as the destroy method is invoked programatically, the servlet instance is not marked and taken out of service.
[ February 03, 2006: Message edited by: karthi keyan ]

http://www.skillassert.com


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Since your code should never be constructng or destroying servlets, what use would all of that be?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
Logically or in practice obtaining the resources (like file handles, threads) should be done in init method and cleaning up in destroy. But the web container does execute the code (if the code contains constructor)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Originally posted by karthi keyan:
But the web container does execute the code


My point exactly. Since web application code should never construct a servlet, what's the use of defining extra constructors?
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
What's a non-parameterized constructor? I've never defined any constructor in servlet, however I guess that Java adds such by default, otherwise where class members will obtain initial values.
You can call destroy method whatever you want. A servlet container can't intercept it in anyway. You can even specify your class name upfront a call to be protected against that servlet container extended your class and overriden destroy method.
Generally I've seen similar questions in some recent interview screening, however they looked as can constructor call finallize().

Edit: Unless you use aspect programming servlet container which can modify your bytecode to eliminate questionable constructions.
[ February 03, 2006: Message edited by: dema rogatkin ]

Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
I know that we can write a non-parameterized(default) constructor in a servlet.I also know that container will default constructor and intialization is done in init() method.

But my question is,if I call servlet's destroy method in this default constructor.
what will happen?Will it be executed?
Will it destroy the instance of the servlet?
Will it compile?
Will it throw some exception at run time?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It won't unload the servlet, it will just execute the code. The destroy() method is a hook for the container to tell the servlet that it is coming out of service and gives it a chance to clean itself up first, releasing resources and whatever else...

Actually I just checked, and I should have just quoted the API:
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.


Note that it always refers to the container calling this method. If ytou call it yourself, the cleanup stuff in the method will still run, but the servlet will not be taken out of service since the method does not then call back to the container, only the other way around.

Dave
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
My point exactly. Since web application code should never construct a servlet, what's the use of defining extra constructors?


I know that servlets container has non-parameterized contructor(default) and it will call and create an instance of it.

Developer is allowed to write non-parametrized(default) constructor in servlet and it will not pose any error.It will be executed.

If a user has written his own non-parametrized(default) constructor and try to call destroy method in that constructor. what will happen?
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
but the servlet will not be taken out of service since the method does not then call back to the container, only the other way around.


Thank you very much!!!

Could you please elaborate more on this quote?

Will it call service method of the servlet and subsequently call GET/POST methods of that servlet?
Or the servlet will be out of service until and otherwise I take out the destroy method from the constructor?

If I define my own default constructor,will it call Service and other methods(GET/POST)?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
If I define my own default constructor,will it call Service and other methods(GET/POST)?


You need to take a step back and understand the overall picture. You seem to be operating under some misconceptions.

A servlet operates in the environment of a servlet container. It is the servlet container that controls construction of an instance of your servlet, calls init, service (which calls doPost etc) and destroy.

Go study the JavaDocs, starting with the javax.servlet.Servlet interface. The API document downloadable from Sun, would also be a good thing to have.

Bill
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

If a user has written his own non-parametrized(default) constructor and try to call destroy method in that constructor. what will happen?

The code in destroy gets executed.


Groovy
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
William Brogden

If you do not know the answer for the question,let others answer my question.
It's question,not a question about the life cycle of servlet.

There are some guys who sincerely try to help me out in finding out the answer.Do not discourage them.

You did not even understand my question.Whatever you have mentioned in your mail is not useful.

Hello David O'Meara and Pradip Bhat...I appreciate your answers
Could you please answer my following questions?
I came to know that container will not pose any error and it will execute the destroy method.
Will it destroy the servlet ot take it out of service?
Will it process GET and SET methods?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I came to know that container will not pose any error and it will execute the destroy method.
Will it destroy the servlet ot take it out of service?
Will it process GET and SET methods?


The servlet wont be destroyed. The servlet will process all future requests.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I never read the whole post so sorry if it is irrelevant...

The container doesn't destroy servlet because destroy() method is being called, it calls destroy() method because servlet is being destroyed.

So if you call destroy() method explicitly, it doesn't mean that container will destroy the servlet...

Hope I am right.
[ February 06, 2006: Message edited by: rathi ji ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by thomas davis:
William Brogden

If you do not know the answer for the question,let others answer my question.
It's question,not a question about the life cycle of servlet.

There are some guys who sincerely try to help me out in finding out the answer.Do not discourage them.

You did not even understand my question.Whatever you have mentioned in your mail is not useful.

Hello David O'Meara and Pradip Bhat...I appreciate your answers
Could you please answer my following questions?
I came to know that container will not pose any error and it will execute the destroy method.
Will it destroy the servlet ot take it out of service?
Will it process GET and SET methods?


Thomas,
First and foremost, we have two rules on the ranch and nunber one is "Be Nice". This tone will not be tolerated here.

Second, questions about creating servlets, destroying servlets, and 'when the service methods can be called' most certainly are lifecycle questions.
The advice William gave you is the same I would have given.
Your question indicates a clear need for stepping back and reading up on the lifecycle of a servlet.
[ February 06, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I missed that response, it makes me sad

Thomas, I have the greatest respect for William. His Servlet knowledge is possibly unsurpassed at the Ranch. Just because I repect his opinion doesn't mean you have to, but you will be doing yourself a dis-service if you don't listen when he speaks. Again, my opinion.

Please see Rathi's response as it is a better description of what I was trying to say.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Ditto. To what David and Ben said.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
I would like to take this opportunity to thank William Brogden for his advice.

Please accept my apology for taking things in different perspective which I should not have done.

I respect his knowledge and guidance.

I aslo would like to thank Pradip Bhat,rathi ji and David O'Meara for their prompt response on my queries.

I would like to take this opportunity to thank William Brogden for his advice.

Please accept my apology for taking things in different perspective which I should not have done.

I respect his knowledge and guidance.

I aslo would like to thank Pradip Bhat,rathi ji and David O'Meara for their prompt response on my queries.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It's all good
Jay Vaghela
Greenhorn

Joined: Feb 15, 2006
Posts: 22
We can have Constructor in servlet, with and without para.
see the code below

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @version 1.0
* @author jayesh vaghela
*/
public class serv1 extends HttpServlet {

public serv1(){System.out.println("hi i m constructor ");}
public serv1(ServletConfig sc)
{
String initparam = sc.getInitParameter("name");
System.out.println("hi i m constructor "+initparam);
destroy();
}

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
private int fInstanceCount;
private static int fClassCount;


/*public void init() throws ServletException {
System.out.println("init method");
System.out.println("fInstanceCount "+fInstanceCount++);
}*/

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html");
ServletConfig sc = getServletConfig();
serv1 sr = new serv1(sc);
serv1 sr1 = new serv1(sc);
sr.destroy();
//sr.getInitParameter("name");



}

public void destroy() {
System.out.println("destroy method.......");
}

}

Here it rais new Question that if all task we can do here using constructor why we need public void init() method with or without parameter? can any one help me out for that.. ?

Thanks
Jayesh Vaghela
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
What do you think happens when the servlet container calls the init method as per the servlet API?
Hint - look at the Javadocs for HttpServlet.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Destroy Method in servlet constructor