• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt regarding init() block execution

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if some sort of exception occurs inside the init() block? Will the container still be able to service client requests? Or do we have to rectify the problem before trying to use the servlet instance?
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of exception are you talking about ? I mean as far as I know , the init() method does not declare any checked exception and all exceptions have to be programmticaly caught inside the method.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saif, I was asked this question in a job interview. The guy asked me that what will happen if an exception occurs inside the init() block? He did not mention which exception. Will you still be able to service the requests made from the browser? I replied in negative. Then he asked me why? I could not answer that question. Any idea?
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the servlet init() block throws an excpetion , then it wont be able to service any requests , that is certain .
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the servlet init() block throws an excpetion , then it wont be able to service any requests , that is certain . Now regarding the reason , from docs we can see

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

1- Throws a ServletException
2- Does not return within a time period defined by the Web server



So , logicaly if ServletException is thrown from init() block , the servlet was never put into service by the container and thus its unable to service any requests.
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing , Ignore my message about the init method not throwing anything ( I couldn't edit my post ) . The init method does throw a ServletException. From javadocs

Throws:
ServletException - if an exception has occurred that interferes with the servlet's normal operation

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic