Originally posted by William Brogden:
I would probably throw a javax.servlet.UnavailableException with an informative message.
Bill
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
You can't. You can only throw some kind of RuntimeException, or an Error. I would usually prefer to make a custom subclass of RuntimeException, so that anyone who wants to catch it can catch it without accidentally catching other RuntimeExceptions like NullPointerException.
However, does throwing an exception here make a difference? It's not clear to me from the specification that throwing an exception in the listener will stop the servlet from being instantiated. It's possible that notification could happen in a separate thread from the servlet instantiation. You might simply prevent any other listeners from beign notified. The fact that there's no checked exception declared by the method means that other components aren't expecting any exception to be thrown here. You can't really be sure how they'll react. I recommend testing this to see - what happens if you throw an exception here? Be aware, though, that the result you get may depend on your web server. (Or maybe it's well-defined in a spec somewhere; I don't really know.)
In general, yes there are times when there is really no point in proceeding after an exception. And in this cases, it may be a valid option to convert the exception to some kind of RuntimeException, or even Error. But be aware that this may not have the desired effect - try it and see. It may be that the best you can do is log the error, and solve the problem when someone looks at the log.
Another possibility you might consider is to use setAttribute() to set some sort of flag in the context. Then the servlet could later retrieve this in the init() method to understand why it's not working. The init() method can then throw UnavailableException to signal that the servlet was never initialized. In fact, the way you've got it written, you don't need any extra flag. Just check if the applicationProperties attribute is null - if it is, then something went wrong in the listener, before the properties got loaded and put in the context.
Having said that though, why not load the properties in the init() method, rather than the ServletContextListener? That way, you can easily throw the UnavailableException in a place where it's expected, where the container will be able to take appropriate action.
[ January 23, 2008: Message edited by: Jim Yingst ]
This is a good reason. With all due respect to Jim, I would under no circumstances put application initialization code into a particular servlet's init() method. That's an artifact of the past before context listeners came onto the scene.The reason I didn't put it in my init() method is because I have more than one servlet, and all these servlets use the same properties.
"I'm not back." - Bill Harding, Twister
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|