Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet init method behavior  RSS feed

 
narain ashwin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a servlet as below,


when this servlet is called the "DivideByZero" exception is printed on the browser. I have read somewhere that if there is an exception in the init() method of the servlet, then it would call the destroy method, but it doesn't happen here. Please explain the action taken by the container when the init() method throws an exception. I am using tomcat.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi narain,

I think, init() will be calling when first call to servlet.before that servlet get instantiated.
That is fine.
After that destroy() is called before servlet object get garbage collected.
That is up to container when it is garbage collected
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you heard is wrong . read the init() here
 
narain ashwin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the info! could someone please explain what happens if an runtime exception arises in the instantiation (Constructor) of the servlet? will the container try to instantiate the servlet again ? or should we bounce the server after fixing the exception?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move the code to the constructor and you will have the answer.

How you want your exceptions handled depends on how you have configured the web.xml. Exceptions caught by the container can be printed to console and the container can then redirect you to an error page if needed.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will the container try to instantiate the servlet again ?

No. A restart of the web app is required (after fixing the problem).
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
will the container try to instantiate the servlet again ?

No. A restart of the web app is required (after fixing the problem).


In fact, in Tomcat, you'll see a message in the log saying that the application startup failed and it will proceed to attempt to startup whatever other webapps remain to be started. Most people restart Tomcat when this happens, but actually, all you really need to do is deploy the offending webapp and start that one app. If you have the default settings, Tomcat will even attempt to start the new deployment automatically when it detects that the WAR has changed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!