【问题标题】:Send Email or Logged Error on 404 and 500 Http Error Spring MVC在 404 和 500 Http 错误 Spring MVC 上发送电子邮件或记录错误
【发布时间】:2011-04-18 01:39:22
【问题描述】:

我一直在想,当一个特定的 HTTP 错误代码发生时,我怎么可能发送电子邮件 在我的 Spring MVC Web 应用程序中。 请看一下我的 web.xml 配置。它工作正常并将我重定向到特定的错误页面。

<web-app ...>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/404.jsp</location>
  </error-page>

  <error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/pages/500.jsp</location>
  </error-page>

</web-app>

但我想要的是,我想发送电子邮件或记录此特定错误的发生。我在之前的问题中被告知 我的控制器层次结构没有处理这种类型的错误。

有什么想法吗?

【问题讨论】:

  • 4nn 错误(客户端错误)并不“容易”上钩。首先这个问题:你想独立于 servletcontainer 吗?或者您是否愿意使用 servletcontainer 特定的解决方案?例如。仅限 Apache Tomcat。
  • @Balusc 目前我们正在使用 Apache Tomcat。有什么建议吗?

标签: java spring tomcat servlets spring-mvc


【解决方案1】:

对于4nn(客户端)错误,最好的办法是在 servletcontainer 级别配置访问日志。在 Tomcat 的情况下,您可以使用 Valve 来执行此操作。它或多或少是一种Filter,将在servletcontainer 级别而不是在webapp 级别插入。您可以在Apache Tomcat Configuration Reference 中找到更多详细信息。您需要打开并配置Access Log Valve。这只会写入日志文件,但您可以扩展org.apache.catalina.valves.AccessLogValve 并添加一些代码,以在404 的情况下发送邮件。要让它运行,只需将该类放在 Tomcat 的类路径中,并在 Valve 配置中将其指定为 className

对于5nn(服务器端)错误,您可以创建Filter 并将其放在/*url-pattern 上,它在doFilter() 方法中基本上执行以下操作。

try {
    chain.doFilter(request, response);
}
catch (ServletException | IOException e) {
    mail(e);
    throw e;
}

【讨论】:

    【解决方案2】:

    出于某种原因,我做了这样的事情。它适用于我的情况。你能评论我的所作所为吗? 在我的 web.xml 中,我编辑了我的位置标签以将其转发到我的控制器。

     <!-- Error Pages -->
        <error-page>
            <error-code>404</error-code>
            <location>/errorEncountered.htm</location>
        </error-page>
    
        <error-page>
            <error-code>500</error-code>
            <location>/errorEncountered.htm</location>
        </error-page>
    

    在我的控制器中,这是我发送电子邮件的地方。

    @Controller
    @RequestMapping("/errorEncountered.htm")
    public class ErrorHandlerController {
        private MailService mailService;
    
        @RequestMapping(method = RequestMethod.GET)
        public String handleGet(HttpServletRequest request) {
                // The Servlet spec guarantees this attribute will be available
                Throwable exception = (Throwable) request
                        .getAttribute("javax.servlet.error.exception");
    
                .
                .
                .
                mailService.sendMail();
            }
        }
    }
    

    有什么批评吗?

    【讨论】:

    • 有什么批评吗?是的。发送电子邮件是比日志记录更繁重的操作,因此如果您的应用程序会吸引很多访问者,并且在某些时候链接断开或更改了链接(因此来自 google 的流量变为 404),它将使您的应用程序陷入困境。
    【解决方案3】:

    使用web.xml 时,Spring 不会排在第一位(也取决于您的&lt;url-pattern&gt;s)。

    您可以做的是编写一个扩展 HTTPServlet 的自定义 servlet。

    在该 servlet 中,您可以编写代码以在您从 HttpServlet 覆盖的 doGet() 方法中发送电子邮件。

    编辑您的 web.xml &lt;url-pattern&gt; 以在发生错误时转到该 servlet。

    如果是 404 错误代码,您已经知道问题与未找到的页面有关。

    酷。

    【讨论】:

      【解决方案4】:

      在过程中发送有关 Http 错误代码的电子邮件可能为时已晚,无法找到错误发生的原因。对我们来说,更好的做法是发送有关例外情况的电子邮件。我们可能不想对每个例外都这样做,因为那将是压倒性的。

      为此,我们扩展了 SimpleMappingExceptionResolver。然后需要使用上下文初始化此解析器。

      MyExceptionResolver extends SimpleMappingExceptionResolver
      {
          @Autowire
          MailService mailService;
      
          protected ModelAndView getModelAndView(String viewName, Exception ex, HttpServletRequest request)
          {
             //Depending on exception resolve to view name, either 404 or server crash report
      
             //Also send emails for specific exception types.
          } 
      }
      
       Spring wiring:
       <bean class="x.y.z.MyExceptionResolver"/>
      

      此外,我们可以使用方面进行异常处理。您可以为此查找方面文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-05
        • 2012-08-25
        • 2011-09-08
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多