aspose file tools
The moose likes Servlets and the fly likes Constructor and destroy method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Constructor and destroy method" Watch "Constructor and destroy method" New topic
Author

Constructor and destroy method

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hello,

What will happen in the following cases -

1.) if we define a default constructor.
2.) if we define a constructor with arguments.
3.) if we call the destroy() method from the init(), service, doXXX methods



Thanks,
Neeraj.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Neeraj Vij wrote:
1.) if we define a default constructor.

servlet runs without any problem

Neeraj Vij wrote:
2.) if we define a constructor with arguments.


no problem, but you should give default constructor

Neeraj Vij wrote:
3.) if we call the destroy() method from the init(), service, doXXX methods


this question discussed in servlet forum many time. search in javaranch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "few doubts" is not helpful. What would happen if all posts had such a title?

Please go back and change your post to add a more meaningful subject by clicking the button on your post.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

seetharaman venkatasamy wrote:no problem, but you should give default constructor
I'm not sure that I agree.

Constructors, in general, are not very useful in servlets. Most useful initialization work must wait until the init() life-cycle method is called (so that access to the servlet config and the servlet context are available). As such, I cannot recall a single instance where I've defined a servlet constructor.

I don't think that it's particularly useful to define a nullary (no args) constructor when one will be automatically provided by default; that is, unless you have something useful (unlikely) to do in it.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm sure that I disagree.

In Java, the default, no-arg constructor is only created for you if you don't explicitly create any other constructors.
So, if you create a constructor that takes arguments (which would be absolutely useless) and don't explicitly create a zero arg constructor, the container will not be able to instanciate your servlet.

The container calls the zero arg constructor.

If you want to pass something to your servlet, use the init method.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

Ben Souther wrote:I'm sure that I disagree.
I was being uncharacteristically diplomatic.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Neeraj Vij wrote:3.) if we call the destroy() method from the init(), service, doXXX methods

It's just Java. If you call the destroy() method, the code in that method will be executed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

Paul Clapham wrote:
Neeraj Vij wrote:3.) if we call the destroy() method from the init(), service, doXXX methods

It's just Java. If you call the destroy() method, the code in that method will be executed.
Yeah, this has been covered a bazillion times.

Calling destroy() will not cause the servlet to be destroyed. The container merely calls this method when it is taking the servlet out of service.

It's badly named, in my opinion. Were I setting up the API, it'd be called onDestroy() or something like that.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ben Souther wrote:
if you create a constructor that takes arguments (which would be absolutely useless) and don't explicitly create a zero arg constructor, the container will not be able to instanciate your servlet.


thats what i mean "but you should give default constructor"[sorry instead of no-arg i type default ] . i mentioned no problem means you can give argument constructor. but no-arg constructor should be there
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Changes 'should' to 'must' and you'll be right on the money.
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hi,

I have changed the subject. Sorry for the inconvinience caused.

Thanks for the inputs.

I was checking few other sites for the answer on constructor questions. There was no clear answer on it. Some of the forums mentioned that It does not even compile if you provide arg constructor.

what I am able to get it is -

1.) servlet will be unloaded when destroy() will be called by container not our code.
2.) if we give non-arg based constructor. servlet will be instantiated properly and work normally.
3.) if we give arg-based constructor, it will give a runtime exception and not a compile time exception. servlet will not be instantiated.

4.) servlet will be instanitiated if we give both (no/arg) based constructor.

Thanks,
Neeraj.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

#1 is backward.

The container calls the destroy method when the servlet is being taken out of service.
The calling of the destroy method (by the container or from your code), in and of itself, does not destroy the servlet.

See Bear's comment on the naming of the service methods and think of destroy as onDestroy.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ben Souther wrote:Changes 'should' to 'must' and you'll be right on the money.


Exactly Ben,thanks . Trying to improve my English Communication
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ben Souther wrote:Changes 'should' to 'must' and you'll be right on the money.


Exactly Ben,thanks . Trying to improve my English Communication
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
seems like Home work questions , why don't you try it and post what is happening ???


If you want something you never had do something which you had never done
 
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.
 
subject: Constructor and destroy method
 
Similar Threads
Destroy Method in servlet constructor
destroy() method of servlets
destroy() method
constructor for a servlet
Servlet's init() and destroy() method and constructors