如果我们抛出了 ServletException,如何在日志中写入任何内容?

发布于 2024-12-28 07:44:31 字数 335 浏览 0 评论 0原文

如果我的 servlet 抛出 ServletException,我想在日志中添加一个条目

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException{
    try {

    } catch (ServletException e) {
        log.warn("error");
        throw new SerletException(e);
    }

它会递归吗?这是处理异常的正确方法吗?

I want to make an entry in the log if my servlet throws ServletException

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException{
    try {

    } catch (ServletException e) {
        log.warn("error");
        throw new SerletException(e);
    }

Will it recurse? Is it the right way to handle exceptions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2025-01-04 07:44:31

不,不会有递归,但是没有必要将异常包装在另一个递归中。只需简单地抛出e即可。

try {

} catch (ServletException e) {
    log.warn("error");
    throw e;
}

顺便说一句,更好的地方是 Filter,它映射到 /* URL 模式,这样您就不需要在所有 servlet 中重复它。

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    log.warn("error");
    throw e;
}

No, there will be no recursion, but wrapping the exception in another one is unnecessary. Just throw e plain.

try {

} catch (ServletException e) {
    log.warn("error");
    throw e;
}

By the way, a better place for this is a Filter which is mapped on an URL pattern of /*, so that you don't need to repeat it in all servlets.

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    log.warn("error");
    throw e;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文