aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Handling Exception in ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Handling Exception in ServletContextListener" Watch "Handling Exception in ServletContextListener" New topic
Author

Handling Exception in ServletContextListener

Hung Sam
Greenhorn

Joined: Aug 23, 2004
Posts: 6
Hi,

can anyone suggest a way to handle exception in ServletContextListener?

I have a ServeltContextListener class that creates a database connection, then sets that connection as an attribute in ServletContext. The code for creating the database connection may throw exception (FileNotFoundException, IOException, etc). The question is should we catch these exceptions? and if yes what is the best way to do that?

thanks a lot!
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by Hung Sam:
Hi,

can anyone suggest a way to handle exception in ServletContextListener?

I have a ServeltContextListener class that creates a database connection, then sets that connection as an attribute in ServletContext. The code for creating the database connection may throw exception (FileNotFoundException, IOException, etc). The question is should we catch these exceptions? and if yes what is the best way to do that?

thanks a lot!


Of course you should catch the exceptions, but I think the client shouldn't see those. So, because you are setting context attributes (which means information VERY important for all the components of your web-app), in case what you want to achieve cannot be achieved (I mean the creation of a database connection) because a low-level exception, I would set another context attribute, like connectionError to 'true' and then I'd have my application to check this attribute. If its value is not null, I would send a message to the web-app administrator, having him to check the problem. It's likely that the application will have to be restarted, once the problem will be fixed.

I hope that by connection you mean a Datasource reference, rather than a database connection. Usually you don't want to keep a db connection alive
for the web-app lifetime, having it shared with all the web-app components. Rather, you'd like to have access to a datasource object to which you can ask connections when needed. This way, low-level things, like connection pooling, timeout and lifetime will be taken care of the Container, rather than you.

This is if you want to manage db connections from your front-end. If you are preparing for the exam, you will encounter patterns, and then you'll realize that things such as db connections better be in the model of a MVC application. Think that the db management should change without the front-end having to change its code as consequence.


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling Exception in ServletContextListener