我为 JSP 配置了一个过滤器。在过滤器中,我使用 HTTPResponseWrapper 来修改对用户的响应。我在 Filter 的 doFilter 方法中的代码是:
CharArrayWrapper responseWrapper = new CharArrayWrapper(
(HttpServletResponse) response);
chain.doFilter(request, responseWrapper);
String responseString = responseWrapper.toString();
System.out.println("responseString ********************* "
+ responseString);
PrintWriter out = response.getWriter();
out.write(responseString);
CharArrayWrapper 类是:
public class CharArrayWrapper extends HttpServletResponseWrapper {
private CharArrayWriter charWriter;
public CharArrayWrapper(HttpServletResponse response) {
super(response);
charWriter = new CharArrayWriter();
}
public PrintWriter getWriter() {
return (new PrintWriter(charWriter));
}
public String toString() {
return (charWriter.toString());
}
public char[] toCharArray() {
return (charWriter.toCharArray());
}
}
如果配置了上述过滤器的 JSP 执行良好,那么过滤器也没有问题,我可以在控制台上看到 sysout。
另一方面,当 JSP scriptlet 代码(它是一个旧应用程序)中引发一些未处理的异常并且我调用 chain.doFilter(request, responseWrapper); 时,我得到一个 NullPointerException。有什么方法可以让我在过滤器中知道 JSP 中引发了未处理的异常