aspose file tools*
The moose likes Servlets and the fly likes Doubt regarding init() block execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Doubt regarding init() block execution" Watch "Doubt regarding init() block execution" New topic
Author

Doubt regarding init() block execution

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Aug 11, 2011
Posts: 440

If the servlet init() block throws an excpetion , then it wont be able to service any requests , that is certain .
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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

Joined: Aug 11, 2011
Posts: 440

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding init() block execution