This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Constructors

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Zakaria Haque
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic