Send an exception or error message

I would like to receive an email when an error or exception occurs in my web application, so that I can quickly fix it. I installed the JSP error page. I like to show the error page to the client and at the same time receive an email. I use jsp and the corresponding servlet and controller for each jsp.

How can I call a servlet to send email when an error or exception occurs? How is the configuration in web.xml?

Please, help.

+3
source share
8 answers

. , log4j SMTPAppender, , .

+5

, API / (, log4j), , . error-page web.xml, , , log4j, , / . .

Filter url-pattern /* :

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.
    }
}

, :

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();
}

, JSP. exception JSP. . .

<% logger.error(exception); %>
+5

, , , , , web.xml. , Java EE "", .

, , API JavaMail. ( .) /.

, .

, - .

, , .

0

, log4j (@Ben), nagios .

, , :

  • ,

  • ,

  • -

  • , , .

0

log4j , , , , Exception

0

, API .

, , .

. Java EE , JSP http://java.sun.com/javaee/5/docs/tutorial/doc/bnahe.html#bnahi

EDIT: see the documentation on how to do this only with Tomcat at http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html . JavaMail Search.

0
source

Are you using Spring? If so, then you can use the Exception Repository to redirect to a specific controller whenever an error occurs. For JSP errors, you can customize the en error page to send messages.

0
source

Source: https://habr.com/ru/post/1726220/


All Articles