• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Destroy Method in servlet constructor

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since your code should never be constructng or destroying servlets, what use would all of that be?
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ditto. To what David and Ben said.
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all good
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic