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

如果我们抛出了ServletException,如何在日志中写入任何内容?,exception,servlets,log4j,Exception,Servlets,Log4j,如果servlet抛出ServletException,我想在日志中创建一个条目 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{ try { } catch (ServletException e) { log.warn("error"); throw new Serlet

如果servlet抛出ServletException,我想在日志中创建一个条目

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

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

它会重现吗?这是处理异常的正确方法吗?

不,不会有递归,但不需要将异常包装到另一个异常中。只需抛出
e
plain即可

try {

} catch (ServletException e) {
    log.warn("error");
    throw e;
}
顺便说一句,更好的地方是
过滤器
,它映射在
/*
的URL模式上,因此您不需要在所有servlet中重复它

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