wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Constructors" Watch "Servlet Constructors" New topic
Author

Servlet Constructors

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

A servlet container will find a servlet and call Class.forName(servletName).newInstance() and will then manage calling init() for you, but has anyone read about or experimented with supplying a constructor for a servet?
It isn't something I've done and not something I've ever considered doing, but is it valid? is it safe? is it possible?
Anyone have an concrete info on this?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

So I ran a search in this forum, (yeah, I know, I'll run the search first next time) and didn't find anything since Oct 2000.
My thoughts is that in theory, something like this should be safe:

as long as you call the super class in case it needs to do something (what that would be, I'm not sure)
Thoughts or arguments?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12755
    
    5
There is NO GOOD REASON to do that. Remember that a servlet is running in a servlet container environment that does a lot for you, not as a stand alone application.
Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

The benefit that init() gives over a constructor is that a constructor can only be called once! That means that if the server needs to reset things, it can call init() again as opposed to destroying the entire servlet and all its internal objects.
[ January 25, 2002: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
Originally posted by Tim Holloway:
The benefit that init() gives over a constructor is that a constructor can only be called once! That means that if the server needs to reset things, it can call init() again as opposed to destroying the entire servlet and all its internal objects.
[ January 25, 2002: Message edited by: Tim Holloway ]

the servlet spec says
"The servlet container calls the init method exactly once after instantiating
the servlet."
and abotu destroy
"After the servlet container calls this method, it will not call the service
method again on this servlet."
Doesnt that mean "if the server needs to reset things", it has to call destroy, throw away the instance and instatiante and initialize a new object?


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12755
    
    5
Zakaria is correct. If you want to be able to change servlet parameters on the fly you should not do it by re-calling init. Put the parameter reading code in a separate method (which of course can be called by init for startup).
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Constructors
 
Similar Threads
Diff between Servlet and Jsp
ut somehow i canot figure out if they have database support or not conservlet hosting
Acme web server
RMI and Tomcat
Can servlet detect user's clicked "Back" button in browser?