Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding init() block execution

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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?
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic