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?
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 ]
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)
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
posted
0
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?
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
posted
0
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
posted
0
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)?
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.
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?
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.
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 ]
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.
/** * @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(); }
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: 12325
1
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.