send email on exception or error
I'd like to get an email when an error or exception happen in my web application, so that i can do the fix quickly.I've set a JSP error page. I like to show the error page to the client and at the same time get an email. I use jsp and a corresponding servlet and controller for each jsp.
How can i call the servlet to send email when the error or exception occurs? How is the configuration in web.xml?
开发者_JAVA技巧Please help.
You can code your own solution, or you could use the email options in the standard logging packages out there. For example, log4j has SMTPAppender which will do what you need.
That not only depends on what API's and/or frameworks you're already using (e.g. log4j), but it also depends on how you handle the global exceptions. If you just have definied an error-page
in web.xml
, then for example the aforementioned log4j surely won't see/handle it. You need to catch and log it yourself.
You can use a Filter
listening on an url-pattern
of /*
for this:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
try {
chain.doFilter(request, response);
} catch (Exception e) {
logger.error(e); // Let "error" level associate with SMTPAppender.
throw new ServletException(e); // Will be handled by error-page.
}
}
Or in a controller servlet if you have one:
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
View view = new View(request, response);
Action action = ActionFactory.getAction(request);
try {
action.execute(view);
} catch (Exception e) {
logger.error(e); // Let "error" level associate with SMTPAppender.
throw new ServletException(e); // Will be handled by error-page.
}
view.navigate();
}
As a short but semantically nasty alternative you can also make use of scriptlets in JSP. The exception
is available as implicit variable in JSP. Log it in the error page and let the logger handle the mailing stuff. E.g.
<% logger.error(exception); %>
You can use the Java Mail API, javadoc here, and here is a developer guide.
I recommend that you use an existing monitoring solution like log4j (@Ben), nagios or one of the commercially supported open-source products.
If you do roll-your-own solution, watch out for the problems like:
lots of false alarms sending you monitoring SPAM
monitoring SPAM causing you to miss real alarms
lost alarms due to email outages
no alarms because your system has gone catatonic instead of throwing an exception
the log4j solution is particularly good, because you can configure it to also send the previsous log lines so you have more context about how the Exception did happened
Are you using Spring? If yes, then you can use an Exception Resolver to redirect to a particular controller whenever an error occurs. In case of JSP errors, you can configure en error page to send out the mails.
You should investigate thoroughly how to configure an error page in the Servlet API.
In that error page you can do whatever you want to with the triggering exception.
See the Java EE tutorial about how to handle errors in JSP pages at http://java.sun.com/javaee/5/docs/tutorial/doc/bnahe.html#bnahi
EDIT: Also see the documentation of how to do it just with Tomcat at http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html. Search for JavaMail.
I might be wrong, but it seems from your question that you expect it to be a matter of configuration, or adding a few parameters to web.xml
. It's actually somewhat more complicated, since Java EE is more "low-level" than that.
You'll need to write some code that sends an email, using the JavaMail API. (Here's a source example.) That code will have to go in your exception/error handler.
It's a good idea to make this feature configurable, so that you can turn it on or off at different parts of your application.
It's also a good idea to think very carefully what happens when sending the mail fails, for whatever reason.
And, of course, you'll need an existing mail server with an existing mail account.
精彩评论